2 * Copyright (C) 2012 Avionic Design GmbH
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sub license,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the
12 * next paragraph) shall be included in all copies or substantial portions
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 #include <linux/bitops.h>
25 #include <linux/bug.h>
26 #include <linux/errno.h>
27 #include <linux/export.h>
28 #include <linux/hdmi.h>
29 #include <linux/string.h>
31 static void hdmi_infoframe_checksum(void *buffer, size_t size)
37 /* compute checksum */
38 for (i = 0; i < size; i++)
45 * hdmi_avi_infoframe_init() - initialize an HDMI AVI infoframe
46 * @frame: HDMI AVI infoframe
48 * Returns 0 on success or a negative error code on failure.
50 int hdmi_avi_infoframe_init(struct hdmi_avi_infoframe *frame)
52 memset(frame, 0, sizeof(*frame));
54 frame->type = HDMI_INFOFRAME_TYPE_AVI;
56 frame->length = HDMI_AVI_INFOFRAME_SIZE;
60 EXPORT_SYMBOL(hdmi_avi_infoframe_init);
63 * hdmi_avi_infoframe_pack() - write HDMI AVI infoframe to binary buffer
64 * @frame: HDMI AVI infoframe
65 * @buffer: destination buffer
66 * @size: size of buffer
68 * Packs the information contained in the @frame structure into a binary
69 * representation that can be written into the corresponding controller
70 * registers. Also computes the checksum as required by section 5.3.5 of
71 * the HDMI 1.4 specification.
73 * Returns the number of bytes packed into the binary buffer or a negative
74 * error code on failure.
76 ssize_t hdmi_avi_infoframe_pack(struct hdmi_avi_infoframe *frame, void *buffer,
82 length = HDMI_INFOFRAME_HEADER_SIZE + frame->length;
87 memset(buffer, 0, size);
90 ptr[1] = frame->version;
91 ptr[2] = frame->length;
92 ptr[3] = 0; /* checksum */
94 /* start infoframe payload */
95 ptr += HDMI_INFOFRAME_HEADER_SIZE;
97 ptr[0] = ((frame->colorspace & 0x3) << 5) | (frame->scan_mode & 0x3);
99 if (frame->active_info_valid)
102 if (frame->horizontal_bar_valid)
105 if (frame->vertical_bar_valid)
108 ptr[1] = ((frame->colorimetry & 0x3) << 6) |
109 ((frame->picture_aspect & 0x3) << 4) |
110 (frame->active_aspect & 0xf);
112 ptr[2] = ((frame->extended_colorimetry & 0x7) << 4) |
113 ((frame->quantization_range & 0x3) << 2) |
119 ptr[3] = frame->video_code & 0x7f;
121 ptr[4] = ((frame->ycc_quantization_range & 0x3) << 6) |
122 ((frame->content_type & 0x3) << 4) |
123 (frame->pixel_repeat & 0xf);
125 ptr[5] = frame->top_bar & 0xff;
126 ptr[6] = (frame->top_bar >> 8) & 0xff;
127 ptr[7] = frame->bottom_bar & 0xff;
128 ptr[8] = (frame->bottom_bar >> 8) & 0xff;
129 ptr[9] = frame->left_bar & 0xff;
130 ptr[10] = (frame->left_bar >> 8) & 0xff;
131 ptr[11] = frame->right_bar & 0xff;
132 ptr[12] = (frame->right_bar >> 8) & 0xff;
134 hdmi_infoframe_checksum(buffer, length);
138 EXPORT_SYMBOL(hdmi_avi_infoframe_pack);
141 * hdmi_spd_infoframe_init() - initialize an HDMI SPD infoframe
142 * @frame: HDMI SPD infoframe
143 * @vendor: vendor string
144 * @product: product string
146 * Returns 0 on success or a negative error code on failure.
148 int hdmi_spd_infoframe_init(struct hdmi_spd_infoframe *frame,
149 const char *vendor, const char *product)
151 memset(frame, 0, sizeof(*frame));
153 frame->type = HDMI_INFOFRAME_TYPE_SPD;
155 frame->length = HDMI_SPD_INFOFRAME_SIZE;
157 strncpy(frame->vendor, vendor, sizeof(frame->vendor));
158 strncpy(frame->product, product, sizeof(frame->product));
162 EXPORT_SYMBOL(hdmi_spd_infoframe_init);
165 * hdmi_spd_infoframe_pack() - write HDMI SPD infoframe to binary buffer
166 * @frame: HDMI SPD infoframe
167 * @buffer: destination buffer
168 * @size: size of buffer
170 * Packs the information contained in the @frame structure into a binary
171 * representation that can be written into the corresponding controller
172 * registers. Also computes the checksum as required by section 5.3.5 of
173 * the HDMI 1.4 specification.
175 * Returns the number of bytes packed into the binary buffer or a negative
176 * error code on failure.
178 ssize_t hdmi_spd_infoframe_pack(struct hdmi_spd_infoframe *frame, void *buffer,
184 length = HDMI_INFOFRAME_HEADER_SIZE + frame->length;
189 memset(buffer, 0, size);
191 ptr[0] = frame->type;
192 ptr[1] = frame->version;
193 ptr[2] = frame->length;
194 ptr[3] = 0; /* checksum */
196 /* start infoframe payload */
197 ptr += HDMI_INFOFRAME_HEADER_SIZE;
199 memcpy(ptr, frame->vendor, sizeof(frame->vendor));
200 memcpy(ptr + 8, frame->product, sizeof(frame->product));
202 ptr[24] = frame->sdi;
204 hdmi_infoframe_checksum(buffer, length);
208 EXPORT_SYMBOL(hdmi_spd_infoframe_pack);
211 * hdmi_audio_infoframe_init() - initialize an HDMI audio infoframe
212 * @frame: HDMI audio infoframe
214 * Returns 0 on success or a negative error code on failure.
216 int hdmi_audio_infoframe_init(struct hdmi_audio_infoframe *frame)
218 memset(frame, 0, sizeof(*frame));
220 frame->type = HDMI_INFOFRAME_TYPE_AUDIO;
222 frame->length = HDMI_AUDIO_INFOFRAME_SIZE;
226 EXPORT_SYMBOL(hdmi_audio_infoframe_init);
229 * hdmi_audio_infoframe_pack() - write HDMI audio infoframe to binary buffer
230 * @frame: HDMI audio infoframe
231 * @buffer: destination buffer
232 * @size: size of buffer
234 * Packs the information contained in the @frame structure into a binary
235 * representation that can be written into the corresponding controller
236 * registers. Also computes the checksum as required by section 5.3.5 of
237 * the HDMI 1.4 specification.
239 * Returns the number of bytes packed into the binary buffer or a negative
240 * error code on failure.
242 ssize_t hdmi_audio_infoframe_pack(struct hdmi_audio_infoframe *frame,
243 void *buffer, size_t size)
245 unsigned char channels;
249 length = HDMI_INFOFRAME_HEADER_SIZE + frame->length;
254 memset(buffer, 0, size);
256 if (frame->channels >= 2)
257 channels = frame->channels - 1;
261 ptr[0] = frame->type;
262 ptr[1] = frame->version;
263 ptr[2] = frame->length;
264 ptr[3] = 0; /* checksum */
266 /* start infoframe payload */
267 ptr += HDMI_INFOFRAME_HEADER_SIZE;
269 ptr[0] = ((frame->coding_type & 0xf) << 4) | (channels & 0x7);
270 ptr[1] = ((frame->sample_frequency & 0x7) << 2) |
271 (frame->sample_size & 0x3);
272 ptr[2] = frame->coding_type_ext & 0x1f;
273 ptr[3] = frame->channel_allocation;
274 ptr[4] = (frame->level_shift_value & 0xf) << 3;
276 if (frame->downmix_inhibit)
279 hdmi_infoframe_checksum(buffer, length);
283 EXPORT_SYMBOL(hdmi_audio_infoframe_pack);
286 * hdmi_vendor_infoframe_pack() - write a HDMI vendor infoframe to binary
288 * @frame: HDMI vendor infoframe
289 * @buffer: destination buffer
290 * @size: size of buffer
292 * Packs the information contained in the @frame structure into a binary
293 * representation that can be written into the corresponding controller
294 * registers. Also computes the checksum as required by section 5.3.5 of
295 * the HDMI 1.4 specification.
297 * Returns the number of bytes packed into the binary buffer or a negative
298 * error code on failure.
300 ssize_t hdmi_vendor_infoframe_pack(struct hdmi_vendor_infoframe *frame,
301 void *buffer, size_t size)
306 length = HDMI_INFOFRAME_HEADER_SIZE + frame->length;
311 memset(buffer, 0, size);
313 ptr[0] = frame->type;
314 ptr[1] = frame->version;
315 ptr[2] = frame->length;
316 ptr[3] = 0; /* checksum */
318 memcpy(&ptr[HDMI_INFOFRAME_HEADER_SIZE], frame->data, frame->length);
320 hdmi_infoframe_checksum(buffer, length);
324 EXPORT_SYMBOL(hdmi_vendor_infoframe_pack);
327 * hdmi_infoframe_pack() - write a HDMI infoframe to binary buffer
328 * @frame: HDMI infoframe
329 * @buffer: destination buffer
330 * @size: size of buffer
332 * Packs the information contained in the @frame structure into a binary
333 * representation that can be written into the corresponding controller
334 * registers. Also computes the checksum as required by section 5.3.5 of
335 * the HDMI 1.4 specification.
337 * Returns the number of bytes packed into the binary buffer or a negative
338 * error code on failure.
341 hdmi_infoframe_pack(union hdmi_infoframe *frame, void *buffer, size_t size)
345 switch (frame->any.type) {
346 case HDMI_INFOFRAME_TYPE_AVI:
347 length = hdmi_avi_infoframe_pack(&frame->avi, buffer, size);
349 case HDMI_INFOFRAME_TYPE_SPD:
350 length = hdmi_spd_infoframe_pack(&frame->spd, buffer, size);
352 case HDMI_INFOFRAME_TYPE_AUDIO:
353 length = hdmi_audio_infoframe_pack(&frame->audio, buffer, size);
355 case HDMI_INFOFRAME_TYPE_VENDOR:
356 length = hdmi_vendor_infoframe_pack(&frame->vendor,
360 WARN(1, "Bad infoframe type %d\n", frame->any.type);
366 EXPORT_SYMBOL(hdmi_infoframe_pack);