• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 Avionic Design GmbH
3  *
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:
10  *
11  * The above copyright notice and this permission notice (including the
12  * next paragraph) shall be included in all copies or substantial portions
13  * of the Software.
14  *
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.
22  */
23 
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>
30 #include <linux/device.h>
31 
32 #define hdmi_log(fmt, ...) dev_printk(level, dev, fmt, ##__VA_ARGS__)
33 
hdmi_infoframe_checksum(u8 * ptr,size_t size)34 static u8 hdmi_infoframe_checksum(u8 *ptr, size_t size)
35 {
36 	u8 csum = 0;
37 	size_t i;
38 
39 	/* compute checksum */
40 	for (i = 0; i < size; i++)
41 		csum += ptr[i];
42 
43 	return 256 - csum;
44 }
45 
hdmi_infoframe_set_checksum(void * buffer,size_t size)46 static void hdmi_infoframe_set_checksum(void *buffer, size_t size)
47 {
48 	u8 *ptr = buffer;
49 
50 	ptr[3] = hdmi_infoframe_checksum(buffer, size);
51 }
52 
53 /**
54  * hdmi_avi_infoframe_init() - initialize an HDMI AVI infoframe
55  * @frame: HDMI AVI infoframe
56  *
57  * Returns 0 on success or a negative error code on failure.
58  */
hdmi_avi_infoframe_init(struct hdmi_avi_infoframe * frame)59 int hdmi_avi_infoframe_init(struct hdmi_avi_infoframe *frame)
60 {
61 	memset(frame, 0, sizeof(*frame));
62 
63 	frame->type = HDMI_INFOFRAME_TYPE_AVI;
64 	frame->version = 2;
65 	frame->length = HDMI_AVI_INFOFRAME_SIZE;
66 
67 	return 0;
68 }
69 EXPORT_SYMBOL(hdmi_avi_infoframe_init);
70 
71 /**
72  * hdmi_avi_infoframe_pack() - write HDMI AVI infoframe to binary buffer
73  * @frame: HDMI AVI infoframe
74  * @buffer: destination buffer
75  * @size: size of buffer
76  *
77  * Packs the information contained in the @frame structure into a binary
78  * representation that can be written into the corresponding controller
79  * registers. Also computes the checksum as required by section 5.3.5 of
80  * the HDMI 1.4 specification.
81  *
82  * Returns the number of bytes packed into the binary buffer or a negative
83  * error code on failure.
84  */
hdmi_avi_infoframe_pack(struct hdmi_avi_infoframe * frame,void * buffer,size_t size)85 ssize_t hdmi_avi_infoframe_pack(struct hdmi_avi_infoframe *frame, void *buffer,
86 				size_t size)
87 {
88 	u8 *ptr = buffer;
89 	size_t length;
90 
91 	length = HDMI_INFOFRAME_HEADER_SIZE + frame->length;
92 
93 	if (size < length)
94 		return -ENOSPC;
95 
96 	memset(buffer, 0, size);
97 
98 	ptr[0] = frame->type;
99 	ptr[1] = frame->version;
100 	ptr[2] = frame->length;
101 	ptr[3] = 0; /* checksum */
102 
103 	/* start infoframe payload */
104 	ptr += HDMI_INFOFRAME_HEADER_SIZE;
105 
106 	ptr[0] = ((frame->colorspace & 0x3) << 5) | (frame->scan_mode & 0x3);
107 
108 	/*
109 	 * Data byte 1, bit 4 has to be set if we provide the active format
110 	 * aspect ratio
111 	 */
112 	if (frame->active_aspect & 0xf)
113 		ptr[0] |= BIT(4);
114 
115 	/* Bit 3 and 2 indicate if we transmit horizontal/vertical bar data */
116 	if (frame->top_bar || frame->bottom_bar)
117 		ptr[0] |= BIT(3);
118 
119 	if (frame->left_bar || frame->right_bar)
120 		ptr[0] |= BIT(2);
121 
122 	ptr[1] = ((frame->colorimetry & 0x3) << 6) |
123 		 ((frame->picture_aspect & 0x3) << 4) |
124 		 (frame->active_aspect & 0xf);
125 
126 	ptr[2] = ((frame->extended_colorimetry & 0x7) << 4) |
127 		 ((frame->quantization_range & 0x3) << 2) |
128 		 (frame->nups & 0x3);
129 
130 	if (frame->itc)
131 		ptr[2] |= BIT(7);
132 
133 	ptr[3] = frame->video_code & 0x7f;
134 
135 	ptr[4] = ((frame->ycc_quantization_range & 0x3) << 6) |
136 		 ((frame->content_type & 0x3) << 4) |
137 		 (frame->pixel_repeat & 0xf);
138 
139 	ptr[5] = frame->top_bar & 0xff;
140 	ptr[6] = (frame->top_bar >> 8) & 0xff;
141 	ptr[7] = frame->bottom_bar & 0xff;
142 	ptr[8] = (frame->bottom_bar >> 8) & 0xff;
143 	ptr[9] = frame->left_bar & 0xff;
144 	ptr[10] = (frame->left_bar >> 8) & 0xff;
145 	ptr[11] = frame->right_bar & 0xff;
146 	ptr[12] = (frame->right_bar >> 8) & 0xff;
147 
148 	hdmi_infoframe_set_checksum(buffer, length);
149 
150 	return length;
151 }
152 EXPORT_SYMBOL(hdmi_avi_infoframe_pack);
153 
154 /**
155  * hdmi_spd_infoframe_init() - initialize an HDMI SPD infoframe
156  * @frame: HDMI SPD infoframe
157  * @vendor: vendor string
158  * @product: product string
159  *
160  * Returns 0 on success or a negative error code on failure.
161  */
hdmi_spd_infoframe_init(struct hdmi_spd_infoframe * frame,const char * vendor,const char * product)162 int hdmi_spd_infoframe_init(struct hdmi_spd_infoframe *frame,
163 			    const char *vendor, const char *product)
164 {
165 	memset(frame, 0, sizeof(*frame));
166 
167 	frame->type = HDMI_INFOFRAME_TYPE_SPD;
168 	frame->version = 1;
169 	frame->length = HDMI_SPD_INFOFRAME_SIZE;
170 
171 	strncpy(frame->vendor, vendor, sizeof(frame->vendor));
172 	strncpy(frame->product, product, sizeof(frame->product));
173 
174 	return 0;
175 }
176 EXPORT_SYMBOL(hdmi_spd_infoframe_init);
177 
178 /**
179  * hdmi_spd_infoframe_pack() - write HDMI SPD infoframe to binary buffer
180  * @frame: HDMI SPD infoframe
181  * @buffer: destination buffer
182  * @size: size of buffer
183  *
184  * Packs the information contained in the @frame structure into a binary
185  * representation that can be written into the corresponding controller
186  * registers. Also computes the checksum as required by section 5.3.5 of
187  * the HDMI 1.4 specification.
188  *
189  * Returns the number of bytes packed into the binary buffer or a negative
190  * error code on failure.
191  */
hdmi_spd_infoframe_pack(struct hdmi_spd_infoframe * frame,void * buffer,size_t size)192 ssize_t hdmi_spd_infoframe_pack(struct hdmi_spd_infoframe *frame, void *buffer,
193 				size_t size)
194 {
195 	u8 *ptr = buffer;
196 	size_t length;
197 
198 	length = HDMI_INFOFRAME_HEADER_SIZE + frame->length;
199 
200 	if (size < length)
201 		return -ENOSPC;
202 
203 	memset(buffer, 0, size);
204 
205 	ptr[0] = frame->type;
206 	ptr[1] = frame->version;
207 	ptr[2] = frame->length;
208 	ptr[3] = 0; /* checksum */
209 
210 	/* start infoframe payload */
211 	ptr += HDMI_INFOFRAME_HEADER_SIZE;
212 
213 	memcpy(ptr, frame->vendor, sizeof(frame->vendor));
214 	memcpy(ptr + 8, frame->product, sizeof(frame->product));
215 
216 	ptr[24] = frame->sdi;
217 
218 	hdmi_infoframe_set_checksum(buffer, length);
219 
220 	return length;
221 }
222 EXPORT_SYMBOL(hdmi_spd_infoframe_pack);
223 
224 /**
225  * hdmi_audio_infoframe_init() - initialize an HDMI audio infoframe
226  * @frame: HDMI audio infoframe
227  *
228  * Returns 0 on success or a negative error code on failure.
229  */
hdmi_audio_infoframe_init(struct hdmi_audio_infoframe * frame)230 int hdmi_audio_infoframe_init(struct hdmi_audio_infoframe *frame)
231 {
232 	memset(frame, 0, sizeof(*frame));
233 
234 	frame->type = HDMI_INFOFRAME_TYPE_AUDIO;
235 	frame->version = 1;
236 	frame->length = HDMI_AUDIO_INFOFRAME_SIZE;
237 
238 	return 0;
239 }
240 EXPORT_SYMBOL(hdmi_audio_infoframe_init);
241 
242 /**
243  * hdmi_audio_infoframe_pack() - write HDMI audio infoframe to binary buffer
244  * @frame: HDMI audio infoframe
245  * @buffer: destination buffer
246  * @size: size of buffer
247  *
248  * Packs the information contained in the @frame structure into a binary
249  * representation that can be written into the corresponding controller
250  * registers. Also computes the checksum as required by section 5.3.5 of
251  * the HDMI 1.4 specification.
252  *
253  * Returns the number of bytes packed into the binary buffer or a negative
254  * error code on failure.
255  */
hdmi_audio_infoframe_pack(struct hdmi_audio_infoframe * frame,void * buffer,size_t size)256 ssize_t hdmi_audio_infoframe_pack(struct hdmi_audio_infoframe *frame,
257 				  void *buffer, size_t size)
258 {
259 	unsigned char channels;
260 	u8 *ptr = buffer;
261 	size_t length;
262 
263 	length = HDMI_INFOFRAME_HEADER_SIZE + frame->length;
264 
265 	if (size < length)
266 		return -ENOSPC;
267 
268 	memset(buffer, 0, size);
269 
270 	if (frame->channels >= 2)
271 		channels = frame->channels - 1;
272 	else
273 		channels = 0;
274 
275 	ptr[0] = frame->type;
276 	ptr[1] = frame->version;
277 	ptr[2] = frame->length;
278 	ptr[3] = 0; /* checksum */
279 
280 	/* start infoframe payload */
281 	ptr += HDMI_INFOFRAME_HEADER_SIZE;
282 
283 	ptr[0] = ((frame->coding_type & 0xf) << 4) | (channels & 0x7);
284 	ptr[1] = ((frame->sample_frequency & 0x7) << 2) |
285 		 (frame->sample_size & 0x3);
286 	ptr[2] = frame->coding_type_ext & 0x1f;
287 	ptr[3] = frame->channel_allocation;
288 	ptr[4] = (frame->level_shift_value & 0xf) << 3;
289 
290 	if (frame->downmix_inhibit)
291 		ptr[4] |= BIT(7);
292 
293 	hdmi_infoframe_set_checksum(buffer, length);
294 
295 	return length;
296 }
297 EXPORT_SYMBOL(hdmi_audio_infoframe_pack);
298 
299 /**
300  * hdmi_vendor_infoframe_init() - initialize an HDMI vendor infoframe
301  * @frame: HDMI vendor infoframe
302  *
303  * Returns 0 on success or a negative error code on failure.
304  */
hdmi_vendor_infoframe_init(struct hdmi_vendor_infoframe * frame)305 int hdmi_vendor_infoframe_init(struct hdmi_vendor_infoframe *frame)
306 {
307 	memset(frame, 0, sizeof(*frame));
308 
309 	frame->type = HDMI_INFOFRAME_TYPE_VENDOR;
310 	frame->version = 1;
311 
312 	frame->oui = HDMI_IEEE_OUI;
313 
314 	/*
315 	 * 0 is a valid value for s3d_struct, so we use a special "not set"
316 	 * value
317 	 */
318 	frame->s3d_struct = HDMI_3D_STRUCTURE_INVALID;
319 
320 	return 0;
321 }
322 EXPORT_SYMBOL(hdmi_vendor_infoframe_init);
323 
hdmi_vendor_infoframe_length(const struct hdmi_vendor_infoframe * frame)324 static int hdmi_vendor_infoframe_length(const struct hdmi_vendor_infoframe *frame)
325 {
326 	/* for side by side (half) we also need to provide 3D_Ext_Data */
327 	if (frame->s3d_struct >= HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF)
328 		return 6;
329 	else if (frame->vic != 0 || frame->s3d_struct != HDMI_3D_STRUCTURE_INVALID)
330 		return 5;
331 	else
332 		return 4;
333 }
334 
335 /**
336  * hdmi_vendor_infoframe_pack() - write a HDMI vendor infoframe to binary buffer
337  * @frame: HDMI infoframe
338  * @buffer: destination buffer
339  * @size: size of buffer
340  *
341  * Packs the information contained in the @frame structure into a binary
342  * representation that can be written into the corresponding controller
343  * registers. Also computes the checksum as required by section 5.3.5 of
344  * the HDMI 1.4 specification.
345  *
346  * Returns the number of bytes packed into the binary buffer or a negative
347  * error code on failure.
348  */
hdmi_vendor_infoframe_pack(struct hdmi_vendor_infoframe * frame,void * buffer,size_t size)349 ssize_t hdmi_vendor_infoframe_pack(struct hdmi_vendor_infoframe *frame,
350 				 void *buffer, size_t size)
351 {
352 	u8 *ptr = buffer;
353 	size_t length;
354 
355 	/* only one of those can be supplied */
356 	if (frame->vic != 0 && frame->s3d_struct != HDMI_3D_STRUCTURE_INVALID)
357 		return -EINVAL;
358 
359 	frame->length = hdmi_vendor_infoframe_length(frame);
360 
361 	length = HDMI_INFOFRAME_HEADER_SIZE + frame->length;
362 
363 	if (size < length)
364 		return -ENOSPC;
365 
366 	memset(buffer, 0, size);
367 
368 	ptr[0] = frame->type;
369 	ptr[1] = frame->version;
370 	ptr[2] = frame->length;
371 	ptr[3] = 0; /* checksum */
372 
373 	/* HDMI OUI */
374 	ptr[4] = 0x03;
375 	ptr[5] = 0x0c;
376 	ptr[6] = 0x00;
377 
378 	if (frame->s3d_struct != HDMI_3D_STRUCTURE_INVALID) {
379 		ptr[7] = 0x2 << 5;	/* video format */
380 		ptr[8] = (frame->s3d_struct & 0xf) << 4;
381 		if (frame->s3d_struct >= HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF)
382 			ptr[9] = (frame->s3d_ext_data & 0xf) << 4;
383 	} else if (frame->vic) {
384 		ptr[7] = 0x1 << 5;	/* video format */
385 		ptr[8] = frame->vic;
386 	} else {
387 		ptr[7] = 0x0 << 5;	/* video format */
388 	}
389 
390 	hdmi_infoframe_set_checksum(buffer, length);
391 
392 	return length;
393 }
394 EXPORT_SYMBOL(hdmi_vendor_infoframe_pack);
395 
396 /*
397  * hdmi_vendor_any_infoframe_pack() - write a vendor infoframe to binary buffer
398  */
399 static ssize_t
hdmi_vendor_any_infoframe_pack(union hdmi_vendor_any_infoframe * frame,void * buffer,size_t size)400 hdmi_vendor_any_infoframe_pack(union hdmi_vendor_any_infoframe *frame,
401 			   void *buffer, size_t size)
402 {
403 	/* we only know about HDMI vendor infoframes */
404 	if (frame->any.oui != HDMI_IEEE_OUI)
405 		return -EINVAL;
406 
407 	return hdmi_vendor_infoframe_pack(&frame->hdmi, buffer, size);
408 }
409 
410 /**
411  * hdmi_infoframe_pack() - write a HDMI infoframe to binary buffer
412  * @frame: HDMI infoframe
413  * @buffer: destination buffer
414  * @size: size of buffer
415  *
416  * Packs the information contained in the @frame structure into a binary
417  * representation that can be written into the corresponding controller
418  * registers. Also computes the checksum as required by section 5.3.5 of
419  * the HDMI 1.4 specification.
420  *
421  * Returns the number of bytes packed into the binary buffer or a negative
422  * error code on failure.
423  */
424 ssize_t
hdmi_infoframe_pack(union hdmi_infoframe * frame,void * buffer,size_t size)425 hdmi_infoframe_pack(union hdmi_infoframe *frame, void *buffer, size_t size)
426 {
427 	ssize_t length;
428 
429 	switch (frame->any.type) {
430 	case HDMI_INFOFRAME_TYPE_AVI:
431 		length = hdmi_avi_infoframe_pack(&frame->avi, buffer, size);
432 		break;
433 	case HDMI_INFOFRAME_TYPE_SPD:
434 		length = hdmi_spd_infoframe_pack(&frame->spd, buffer, size);
435 		break;
436 	case HDMI_INFOFRAME_TYPE_AUDIO:
437 		length = hdmi_audio_infoframe_pack(&frame->audio, buffer, size);
438 		break;
439 	case HDMI_INFOFRAME_TYPE_VENDOR:
440 		length = hdmi_vendor_any_infoframe_pack(&frame->vendor,
441 							buffer, size);
442 		break;
443 	default:
444 		WARN(1, "Bad infoframe type %d\n", frame->any.type);
445 		length = -EINVAL;
446 	}
447 
448 	return length;
449 }
450 EXPORT_SYMBOL(hdmi_infoframe_pack);
451 
hdmi_infoframe_type_get_name(enum hdmi_infoframe_type type)452 static const char *hdmi_infoframe_type_get_name(enum hdmi_infoframe_type type)
453 {
454 	if (type < 0x80 || type > 0x9f)
455 		return "Invalid";
456 	switch (type) {
457 	case HDMI_INFOFRAME_TYPE_VENDOR:
458 		return "Vendor";
459 	case HDMI_INFOFRAME_TYPE_AVI:
460 		return "Auxiliary Video Information (AVI)";
461 	case HDMI_INFOFRAME_TYPE_SPD:
462 		return "Source Product Description (SPD)";
463 	case HDMI_INFOFRAME_TYPE_AUDIO:
464 		return "Audio";
465 	}
466 	return "Reserved";
467 }
468 
hdmi_infoframe_log_header(const char * level,struct device * dev,struct hdmi_any_infoframe * frame)469 static void hdmi_infoframe_log_header(const char *level,
470 				      struct device *dev,
471 				      struct hdmi_any_infoframe *frame)
472 {
473 	hdmi_log("HDMI infoframe: %s, version %u, length %u\n",
474 		hdmi_infoframe_type_get_name(frame->type),
475 		frame->version, frame->length);
476 }
477 
hdmi_colorspace_get_name(enum hdmi_colorspace colorspace)478 static const char *hdmi_colorspace_get_name(enum hdmi_colorspace colorspace)
479 {
480 	switch (colorspace) {
481 	case HDMI_COLORSPACE_RGB:
482 		return "RGB";
483 	case HDMI_COLORSPACE_YUV422:
484 		return "YCbCr 4:2:2";
485 	case HDMI_COLORSPACE_YUV444:
486 		return "YCbCr 4:4:4";
487 	case HDMI_COLORSPACE_YUV420:
488 		return "YCbCr 4:2:0";
489 	case HDMI_COLORSPACE_RESERVED4:
490 		return "Reserved (4)";
491 	case HDMI_COLORSPACE_RESERVED5:
492 		return "Reserved (5)";
493 	case HDMI_COLORSPACE_RESERVED6:
494 		return "Reserved (6)";
495 	case HDMI_COLORSPACE_IDO_DEFINED:
496 		return "IDO Defined";
497 	}
498 	return "Invalid";
499 }
500 
hdmi_scan_mode_get_name(enum hdmi_scan_mode scan_mode)501 static const char *hdmi_scan_mode_get_name(enum hdmi_scan_mode scan_mode)
502 {
503 	switch (scan_mode) {
504 	case HDMI_SCAN_MODE_NONE:
505 		return "No Data";
506 	case HDMI_SCAN_MODE_OVERSCAN:
507 		return "Overscan";
508 	case HDMI_SCAN_MODE_UNDERSCAN:
509 		return "Underscan";
510 	case HDMI_SCAN_MODE_RESERVED:
511 		return "Reserved";
512 	}
513 	return "Invalid";
514 }
515 
hdmi_colorimetry_get_name(enum hdmi_colorimetry colorimetry)516 static const char *hdmi_colorimetry_get_name(enum hdmi_colorimetry colorimetry)
517 {
518 	switch (colorimetry) {
519 	case HDMI_COLORIMETRY_NONE:
520 		return "No Data";
521 	case HDMI_COLORIMETRY_ITU_601:
522 		return "ITU601";
523 	case HDMI_COLORIMETRY_ITU_709:
524 		return "ITU709";
525 	case HDMI_COLORIMETRY_EXTENDED:
526 		return "Extended";
527 	}
528 	return "Invalid";
529 }
530 
531 static const char *
hdmi_picture_aspect_get_name(enum hdmi_picture_aspect picture_aspect)532 hdmi_picture_aspect_get_name(enum hdmi_picture_aspect picture_aspect)
533 {
534 	switch (picture_aspect) {
535 	case HDMI_PICTURE_ASPECT_NONE:
536 		return "No Data";
537 	case HDMI_PICTURE_ASPECT_4_3:
538 		return "4:3";
539 	case HDMI_PICTURE_ASPECT_16_9:
540 		return "16:9";
541 	case HDMI_PICTURE_ASPECT_RESERVED:
542 		return "Reserved";
543 	}
544 	return "Invalid";
545 }
546 
547 static const char *
hdmi_active_aspect_get_name(enum hdmi_active_aspect active_aspect)548 hdmi_active_aspect_get_name(enum hdmi_active_aspect active_aspect)
549 {
550 	if (active_aspect < 0 || active_aspect > 0xf)
551 		return "Invalid";
552 
553 	switch (active_aspect) {
554 	case HDMI_ACTIVE_ASPECT_16_9_TOP:
555 		return "16:9 Top";
556 	case HDMI_ACTIVE_ASPECT_14_9_TOP:
557 		return "14:9 Top";
558 	case HDMI_ACTIVE_ASPECT_16_9_CENTER:
559 		return "16:9 Center";
560 	case HDMI_ACTIVE_ASPECT_PICTURE:
561 		return "Same as Picture";
562 	case HDMI_ACTIVE_ASPECT_4_3:
563 		return "4:3";
564 	case HDMI_ACTIVE_ASPECT_16_9:
565 		return "16:9";
566 	case HDMI_ACTIVE_ASPECT_14_9:
567 		return "14:9";
568 	case HDMI_ACTIVE_ASPECT_4_3_SP_14_9:
569 		return "4:3 SP 14:9";
570 	case HDMI_ACTIVE_ASPECT_16_9_SP_14_9:
571 		return "16:9 SP 14:9";
572 	case HDMI_ACTIVE_ASPECT_16_9_SP_4_3:
573 		return "16:9 SP 4:3";
574 	}
575 	return "Reserved";
576 }
577 
578 static const char *
hdmi_extended_colorimetry_get_name(enum hdmi_extended_colorimetry ext_col)579 hdmi_extended_colorimetry_get_name(enum hdmi_extended_colorimetry ext_col)
580 {
581 	switch (ext_col) {
582 	case HDMI_EXTENDED_COLORIMETRY_XV_YCC_601:
583 		return "xvYCC 601";
584 	case HDMI_EXTENDED_COLORIMETRY_XV_YCC_709:
585 		return "xvYCC 709";
586 	case HDMI_EXTENDED_COLORIMETRY_S_YCC_601:
587 		return "sYCC 601";
588 	case HDMI_EXTENDED_COLORIMETRY_ADOBE_YCC_601:
589 		return "Adobe YCC 601";
590 	case HDMI_EXTENDED_COLORIMETRY_ADOBE_RGB:
591 		return "Adobe RGB";
592 	case HDMI_EXTENDED_COLORIMETRY_BT2020_CONST_LUM:
593 		return "BT.2020 Constant Luminance";
594 	case HDMI_EXTENDED_COLORIMETRY_BT2020:
595 		return "BT.2020";
596 	case HDMI_EXTENDED_COLORIMETRY_RESERVED:
597 		return "Reserved";
598 	}
599 	return "Invalid";
600 }
601 
602 static const char *
hdmi_quantization_range_get_name(enum hdmi_quantization_range qrange)603 hdmi_quantization_range_get_name(enum hdmi_quantization_range qrange)
604 {
605 	switch (qrange) {
606 	case HDMI_QUANTIZATION_RANGE_DEFAULT:
607 		return "Default";
608 	case HDMI_QUANTIZATION_RANGE_LIMITED:
609 		return "Limited";
610 	case HDMI_QUANTIZATION_RANGE_FULL:
611 		return "Full";
612 	case HDMI_QUANTIZATION_RANGE_RESERVED:
613 		return "Reserved";
614 	}
615 	return "Invalid";
616 }
617 
hdmi_nups_get_name(enum hdmi_nups nups)618 static const char *hdmi_nups_get_name(enum hdmi_nups nups)
619 {
620 	switch (nups) {
621 	case HDMI_NUPS_UNKNOWN:
622 		return "Unknown Non-uniform Scaling";
623 	case HDMI_NUPS_HORIZONTAL:
624 		return "Horizontally Scaled";
625 	case HDMI_NUPS_VERTICAL:
626 		return "Vertically Scaled";
627 	case HDMI_NUPS_BOTH:
628 		return "Horizontally and Vertically Scaled";
629 	}
630 	return "Invalid";
631 }
632 
633 static const char *
hdmi_ycc_quantization_range_get_name(enum hdmi_ycc_quantization_range qrange)634 hdmi_ycc_quantization_range_get_name(enum hdmi_ycc_quantization_range qrange)
635 {
636 	switch (qrange) {
637 	case HDMI_YCC_QUANTIZATION_RANGE_LIMITED:
638 		return "Limited";
639 	case HDMI_YCC_QUANTIZATION_RANGE_FULL:
640 		return "Full";
641 	}
642 	return "Invalid";
643 }
644 
645 static const char *
hdmi_content_type_get_name(enum hdmi_content_type content_type)646 hdmi_content_type_get_name(enum hdmi_content_type content_type)
647 {
648 	switch (content_type) {
649 	case HDMI_CONTENT_TYPE_GRAPHICS:
650 		return "Graphics";
651 	case HDMI_CONTENT_TYPE_PHOTO:
652 		return "Photo";
653 	case HDMI_CONTENT_TYPE_CINEMA:
654 		return "Cinema";
655 	case HDMI_CONTENT_TYPE_GAME:
656 		return "Game";
657 	}
658 	return "Invalid";
659 }
660 
661 /**
662  * hdmi_avi_infoframe_log() - log info of HDMI AVI infoframe
663  * @level: logging level
664  * @dev: device
665  * @frame: HDMI AVI infoframe
666  */
hdmi_avi_infoframe_log(const char * level,struct device * dev,struct hdmi_avi_infoframe * frame)667 static void hdmi_avi_infoframe_log(const char *level,
668 				   struct device *dev,
669 				   struct hdmi_avi_infoframe *frame)
670 {
671 	hdmi_infoframe_log_header(level, dev,
672 				  (struct hdmi_any_infoframe *)frame);
673 
674 	hdmi_log("    colorspace: %s\n",
675 			hdmi_colorspace_get_name(frame->colorspace));
676 	hdmi_log("    scan mode: %s\n",
677 			hdmi_scan_mode_get_name(frame->scan_mode));
678 	hdmi_log("    colorimetry: %s\n",
679 			hdmi_colorimetry_get_name(frame->colorimetry));
680 	hdmi_log("    picture aspect: %s\n",
681 			hdmi_picture_aspect_get_name(frame->picture_aspect));
682 	hdmi_log("    active aspect: %s\n",
683 			hdmi_active_aspect_get_name(frame->active_aspect));
684 	hdmi_log("    itc: %s\n", frame->itc ? "IT Content" : "No Data");
685 	hdmi_log("    extended colorimetry: %s\n",
686 			hdmi_extended_colorimetry_get_name(frame->extended_colorimetry));
687 	hdmi_log("    quantization range: %s\n",
688 			hdmi_quantization_range_get_name(frame->quantization_range));
689 	hdmi_log("    nups: %s\n", hdmi_nups_get_name(frame->nups));
690 	hdmi_log("    video code: %u\n", frame->video_code);
691 	hdmi_log("    ycc quantization range: %s\n",
692 			hdmi_ycc_quantization_range_get_name(frame->ycc_quantization_range));
693 	hdmi_log("    hdmi content type: %s\n",
694 			hdmi_content_type_get_name(frame->content_type));
695 	hdmi_log("    pixel repeat: %u\n", frame->pixel_repeat);
696 	hdmi_log("    bar top %u, bottom %u, left %u, right %u\n",
697 			frame->top_bar, frame->bottom_bar,
698 			frame->left_bar, frame->right_bar);
699 }
700 
hdmi_spd_sdi_get_name(enum hdmi_spd_sdi sdi)701 static const char *hdmi_spd_sdi_get_name(enum hdmi_spd_sdi sdi)
702 {
703 	if (sdi < 0 || sdi > 0xff)
704 		return "Invalid";
705 	switch (sdi) {
706 	case HDMI_SPD_SDI_UNKNOWN:
707 		return "Unknown";
708 	case HDMI_SPD_SDI_DSTB:
709 		return "Digital STB";
710 	case HDMI_SPD_SDI_DVDP:
711 		return "DVD Player";
712 	case HDMI_SPD_SDI_DVHS:
713 		return "D-VHS";
714 	case HDMI_SPD_SDI_HDDVR:
715 		return "HDD Videorecorder";
716 	case HDMI_SPD_SDI_DVC:
717 		return "DVC";
718 	case HDMI_SPD_SDI_DSC:
719 		return "DSC";
720 	case HDMI_SPD_SDI_VCD:
721 		return "Video CD";
722 	case HDMI_SPD_SDI_GAME:
723 		return "Game";
724 	case HDMI_SPD_SDI_PC:
725 		return "PC General";
726 	case HDMI_SPD_SDI_BD:
727 		return "Blu-Ray Disc (BD)";
728 	case HDMI_SPD_SDI_SACD:
729 		return "Super Audio CD";
730 	case HDMI_SPD_SDI_HDDVD:
731 		return "HD DVD";
732 	case HDMI_SPD_SDI_PMP:
733 		return "PMP";
734 	}
735 	return "Reserved";
736 }
737 
738 /**
739  * hdmi_spd_infoframe_log() - log info of HDMI SPD infoframe
740  * @level: logging level
741  * @dev: device
742  * @frame: HDMI SPD infoframe
743  */
hdmi_spd_infoframe_log(const char * level,struct device * dev,struct hdmi_spd_infoframe * frame)744 static void hdmi_spd_infoframe_log(const char *level,
745 				   struct device *dev,
746 				   struct hdmi_spd_infoframe *frame)
747 {
748 	u8 buf[17];
749 
750 	hdmi_infoframe_log_header(level, dev,
751 				  (struct hdmi_any_infoframe *)frame);
752 
753 	memset(buf, 0, sizeof(buf));
754 
755 	strncpy(buf, frame->vendor, 8);
756 	hdmi_log("    vendor: %s\n", buf);
757 	strncpy(buf, frame->product, 16);
758 	hdmi_log("    product: %s\n", buf);
759 	hdmi_log("    source device information: %s (0x%x)\n",
760 		hdmi_spd_sdi_get_name(frame->sdi), frame->sdi);
761 }
762 
763 static const char *
hdmi_audio_coding_type_get_name(enum hdmi_audio_coding_type coding_type)764 hdmi_audio_coding_type_get_name(enum hdmi_audio_coding_type coding_type)
765 {
766 	switch (coding_type) {
767 	case HDMI_AUDIO_CODING_TYPE_STREAM:
768 		return "Refer to Stream Header";
769 	case HDMI_AUDIO_CODING_TYPE_PCM:
770 		return "PCM";
771 	case HDMI_AUDIO_CODING_TYPE_AC3:
772 		return "AC-3";
773 	case HDMI_AUDIO_CODING_TYPE_MPEG1:
774 		return "MPEG1";
775 	case HDMI_AUDIO_CODING_TYPE_MP3:
776 		return "MP3";
777 	case HDMI_AUDIO_CODING_TYPE_MPEG2:
778 		return "MPEG2";
779 	case HDMI_AUDIO_CODING_TYPE_AAC_LC:
780 		return "AAC";
781 	case HDMI_AUDIO_CODING_TYPE_DTS:
782 		return "DTS";
783 	case HDMI_AUDIO_CODING_TYPE_ATRAC:
784 		return "ATRAC";
785 	case HDMI_AUDIO_CODING_TYPE_DSD:
786 		return "One Bit Audio";
787 	case HDMI_AUDIO_CODING_TYPE_EAC3:
788 		return "Dolby Digital +";
789 	case HDMI_AUDIO_CODING_TYPE_DTS_HD:
790 		return "DTS-HD";
791 	case HDMI_AUDIO_CODING_TYPE_MLP:
792 		return "MAT (MLP)";
793 	case HDMI_AUDIO_CODING_TYPE_DST:
794 		return "DST";
795 	case HDMI_AUDIO_CODING_TYPE_WMA_PRO:
796 		return "WMA PRO";
797 	case HDMI_AUDIO_CODING_TYPE_CXT:
798 		return "Refer to CXT";
799 	}
800 	return "Invalid";
801 }
802 
803 static const char *
hdmi_audio_sample_size_get_name(enum hdmi_audio_sample_size sample_size)804 hdmi_audio_sample_size_get_name(enum hdmi_audio_sample_size sample_size)
805 {
806 	switch (sample_size) {
807 	case HDMI_AUDIO_SAMPLE_SIZE_STREAM:
808 		return "Refer to Stream Header";
809 	case HDMI_AUDIO_SAMPLE_SIZE_16:
810 		return "16 bit";
811 	case HDMI_AUDIO_SAMPLE_SIZE_20:
812 		return "20 bit";
813 	case HDMI_AUDIO_SAMPLE_SIZE_24:
814 		return "24 bit";
815 	}
816 	return "Invalid";
817 }
818 
819 static const char *
hdmi_audio_sample_frequency_get_name(enum hdmi_audio_sample_frequency freq)820 hdmi_audio_sample_frequency_get_name(enum hdmi_audio_sample_frequency freq)
821 {
822 	switch (freq) {
823 	case HDMI_AUDIO_SAMPLE_FREQUENCY_STREAM:
824 		return "Refer to Stream Header";
825 	case HDMI_AUDIO_SAMPLE_FREQUENCY_32000:
826 		return "32 kHz";
827 	case HDMI_AUDIO_SAMPLE_FREQUENCY_44100:
828 		return "44.1 kHz (CD)";
829 	case HDMI_AUDIO_SAMPLE_FREQUENCY_48000:
830 		return "48 kHz";
831 	case HDMI_AUDIO_SAMPLE_FREQUENCY_88200:
832 		return "88.2 kHz";
833 	case HDMI_AUDIO_SAMPLE_FREQUENCY_96000:
834 		return "96 kHz";
835 	case HDMI_AUDIO_SAMPLE_FREQUENCY_176400:
836 		return "176.4 kHz";
837 	case HDMI_AUDIO_SAMPLE_FREQUENCY_192000:
838 		return "192 kHz";
839 	}
840 	return "Invalid";
841 }
842 
843 static const char *
hdmi_audio_coding_type_ext_get_name(enum hdmi_audio_coding_type_ext ctx)844 hdmi_audio_coding_type_ext_get_name(enum hdmi_audio_coding_type_ext ctx)
845 {
846 	if (ctx < 0 || ctx > 0x1f)
847 		return "Invalid";
848 
849 	switch (ctx) {
850 	case HDMI_AUDIO_CODING_TYPE_EXT_CT:
851 		return "Refer to CT";
852 	case HDMI_AUDIO_CODING_TYPE_EXT_HE_AAC:
853 		return "HE AAC";
854 	case HDMI_AUDIO_CODING_TYPE_EXT_HE_AAC_V2:
855 		return "HE AAC v2";
856 	case HDMI_AUDIO_CODING_TYPE_EXT_MPEG_SURROUND:
857 		return "MPEG SURROUND";
858 	case HDMI_AUDIO_CODING_TYPE_EXT_MPEG4_HE_AAC:
859 		return "MPEG-4 HE AAC";
860 	case HDMI_AUDIO_CODING_TYPE_EXT_MPEG4_HE_AAC_V2:
861 		return "MPEG-4 HE AAC v2";
862 	case HDMI_AUDIO_CODING_TYPE_EXT_MPEG4_AAC_LC:
863 		return "MPEG-4 AAC LC";
864 	case HDMI_AUDIO_CODING_TYPE_EXT_DRA:
865 		return "DRA";
866 	case HDMI_AUDIO_CODING_TYPE_EXT_MPEG4_HE_AAC_SURROUND:
867 		return "MPEG-4 HE AAC + MPEG Surround";
868 	case HDMI_AUDIO_CODING_TYPE_EXT_MPEG4_AAC_LC_SURROUND:
869 		return "MPEG-4 AAC LC + MPEG Surround";
870 	}
871 	return "Reserved";
872 }
873 
874 /**
875  * hdmi_audio_infoframe_log() - log info of HDMI AUDIO infoframe
876  * @level: logging level
877  * @dev: device
878  * @frame: HDMI AUDIO infoframe
879  */
hdmi_audio_infoframe_log(const char * level,struct device * dev,struct hdmi_audio_infoframe * frame)880 static void hdmi_audio_infoframe_log(const char *level,
881 				     struct device *dev,
882 				     struct hdmi_audio_infoframe *frame)
883 {
884 	hdmi_infoframe_log_header(level, dev,
885 				  (struct hdmi_any_infoframe *)frame);
886 
887 	if (frame->channels)
888 		hdmi_log("    channels: %u\n", frame->channels - 1);
889 	else
890 		hdmi_log("    channels: Refer to stream header\n");
891 	hdmi_log("    coding type: %s\n",
892 			hdmi_audio_coding_type_get_name(frame->coding_type));
893 	hdmi_log("    sample size: %s\n",
894 			hdmi_audio_sample_size_get_name(frame->sample_size));
895 	hdmi_log("    sample frequency: %s\n",
896 			hdmi_audio_sample_frequency_get_name(frame->sample_frequency));
897 	hdmi_log("    coding type ext: %s\n",
898 			hdmi_audio_coding_type_ext_get_name(frame->coding_type_ext));
899 	hdmi_log("    channel allocation: 0x%x\n",
900 			frame->channel_allocation);
901 	hdmi_log("    level shift value: %u dB\n",
902 			frame->level_shift_value);
903 	hdmi_log("    downmix inhibit: %s\n",
904 			frame->downmix_inhibit ? "Yes" : "No");
905 }
906 
907 static const char *
hdmi_3d_structure_get_name(enum hdmi_3d_structure s3d_struct)908 hdmi_3d_structure_get_name(enum hdmi_3d_structure s3d_struct)
909 {
910 	if (s3d_struct < 0 || s3d_struct > 0xf)
911 		return "Invalid";
912 
913 	switch (s3d_struct) {
914 	case HDMI_3D_STRUCTURE_FRAME_PACKING:
915 		return "Frame Packing";
916 	case HDMI_3D_STRUCTURE_FIELD_ALTERNATIVE:
917 		return "Field Alternative";
918 	case HDMI_3D_STRUCTURE_LINE_ALTERNATIVE:
919 		return "Line Alternative";
920 	case HDMI_3D_STRUCTURE_SIDE_BY_SIDE_FULL:
921 		return "Side-by-side (Full)";
922 	case HDMI_3D_STRUCTURE_L_DEPTH:
923 		return "L + Depth";
924 	case HDMI_3D_STRUCTURE_L_DEPTH_GFX_GFX_DEPTH:
925 		return "L + Depth + Graphics + Graphics-depth";
926 	case HDMI_3D_STRUCTURE_TOP_AND_BOTTOM:
927 		return "Top-and-Bottom";
928 	case HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF:
929 		return "Side-by-side (Half)";
930 	default:
931 		break;
932 	}
933 	return "Reserved";
934 }
935 
936 /**
937  * hdmi_vendor_infoframe_log() - log info of HDMI VENDOR infoframe
938  * @level: logging level
939  * @dev: device
940  * @frame: HDMI VENDOR infoframe
941  */
942 static void
hdmi_vendor_any_infoframe_log(const char * level,struct device * dev,union hdmi_vendor_any_infoframe * frame)943 hdmi_vendor_any_infoframe_log(const char *level,
944 			      struct device *dev,
945 			      union hdmi_vendor_any_infoframe *frame)
946 {
947 	struct hdmi_vendor_infoframe *hvf = &frame->hdmi;
948 
949 	hdmi_infoframe_log_header(level, dev,
950 				  (struct hdmi_any_infoframe *)frame);
951 
952 	if (frame->any.oui != HDMI_IEEE_OUI) {
953 		hdmi_log("    not a HDMI vendor infoframe\n");
954 		return;
955 	}
956 	if (hvf->vic == 0 && hvf->s3d_struct == HDMI_3D_STRUCTURE_INVALID) {
957 		hdmi_log("    empty frame\n");
958 		return;
959 	}
960 
961 	if (hvf->vic)
962 		hdmi_log("    HDMI VIC: %u\n", hvf->vic);
963 	if (hvf->s3d_struct != HDMI_3D_STRUCTURE_INVALID) {
964 		hdmi_log("    3D structure: %s\n",
965 				hdmi_3d_structure_get_name(hvf->s3d_struct));
966 		if (hvf->s3d_struct >= HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF)
967 			hdmi_log("    3D extension data: %d\n",
968 					hvf->s3d_ext_data);
969 	}
970 }
971 
972 /**
973  * hdmi_infoframe_log() - log info of HDMI infoframe
974  * @level: logging level
975  * @dev: device
976  * @frame: HDMI infoframe
977  */
hdmi_infoframe_log(const char * level,struct device * dev,union hdmi_infoframe * frame)978 void hdmi_infoframe_log(const char *level,
979 			struct device *dev,
980 			union hdmi_infoframe *frame)
981 {
982 	switch (frame->any.type) {
983 	case HDMI_INFOFRAME_TYPE_AVI:
984 		hdmi_avi_infoframe_log(level, dev, &frame->avi);
985 		break;
986 	case HDMI_INFOFRAME_TYPE_SPD:
987 		hdmi_spd_infoframe_log(level, dev, &frame->spd);
988 		break;
989 	case HDMI_INFOFRAME_TYPE_AUDIO:
990 		hdmi_audio_infoframe_log(level, dev, &frame->audio);
991 		break;
992 	case HDMI_INFOFRAME_TYPE_VENDOR:
993 		hdmi_vendor_any_infoframe_log(level, dev, &frame->vendor);
994 		break;
995 	}
996 }
997 EXPORT_SYMBOL(hdmi_infoframe_log);
998 
999 /**
1000  * hdmi_avi_infoframe_unpack() - unpack binary buffer to a HDMI AVI infoframe
1001  * @buffer: source buffer
1002  * @frame: HDMI AVI infoframe
1003  *
1004  * Unpacks the information contained in binary @buffer into a structured
1005  * @frame of the HDMI Auxiliary Video (AVI) information frame.
1006  * Also verifies the checksum as required by section 5.3.5 of the HDMI 1.4
1007  * specification.
1008  *
1009  * Returns 0 on success or a negative error code on failure.
1010  */
hdmi_avi_infoframe_unpack(struct hdmi_avi_infoframe * frame,void * buffer)1011 static int hdmi_avi_infoframe_unpack(struct hdmi_avi_infoframe *frame,
1012 				     void *buffer)
1013 {
1014 	u8 *ptr = buffer;
1015 	int ret;
1016 
1017 	if (ptr[0] != HDMI_INFOFRAME_TYPE_AVI ||
1018 	    ptr[1] != 2 ||
1019 	    ptr[2] != HDMI_AVI_INFOFRAME_SIZE)
1020 		return -EINVAL;
1021 
1022 	if (hdmi_infoframe_checksum(buffer, HDMI_INFOFRAME_SIZE(AVI)) != 0)
1023 		return -EINVAL;
1024 
1025 	ret = hdmi_avi_infoframe_init(frame);
1026 	if (ret)
1027 		return ret;
1028 
1029 	ptr += HDMI_INFOFRAME_HEADER_SIZE;
1030 
1031 	frame->colorspace = (ptr[0] >> 5) & 0x3;
1032 	if (ptr[0] & 0x10)
1033 		frame->active_aspect = ptr[1] & 0xf;
1034 	if (ptr[0] & 0x8) {
1035 		frame->top_bar = (ptr[6] << 8) | ptr[5];
1036 		frame->bottom_bar = (ptr[8] << 8) | ptr[7];
1037 	}
1038 	if (ptr[0] & 0x4) {
1039 		frame->left_bar = (ptr[10] << 8) | ptr[9];
1040 		frame->right_bar = (ptr[12] << 8) | ptr[11];
1041 	}
1042 	frame->scan_mode = ptr[0] & 0x3;
1043 
1044 	frame->colorimetry = (ptr[1] >> 6) & 0x3;
1045 	frame->picture_aspect = (ptr[1] >> 4) & 0x3;
1046 	frame->active_aspect = ptr[1] & 0xf;
1047 
1048 	frame->itc = ptr[2] & 0x80 ? true : false;
1049 	frame->extended_colorimetry = (ptr[2] >> 4) & 0x7;
1050 	frame->quantization_range = (ptr[2] >> 2) & 0x3;
1051 	frame->nups = ptr[2] & 0x3;
1052 
1053 	frame->video_code = ptr[3] & 0x7f;
1054 	frame->ycc_quantization_range = (ptr[4] >> 6) & 0x3;
1055 	frame->content_type = (ptr[4] >> 4) & 0x3;
1056 
1057 	frame->pixel_repeat = ptr[4] & 0xf;
1058 
1059 	return 0;
1060 }
1061 
1062 /**
1063  * hdmi_spd_infoframe_unpack() - unpack binary buffer to a HDMI SPD infoframe
1064  * @buffer: source buffer
1065  * @frame: HDMI SPD infoframe
1066  *
1067  * Unpacks the information contained in binary @buffer into a structured
1068  * @frame of the HDMI Source Product Description (SPD) information frame.
1069  * Also verifies the checksum as required by section 5.3.5 of the HDMI 1.4
1070  * specification.
1071  *
1072  * Returns 0 on success or a negative error code on failure.
1073  */
hdmi_spd_infoframe_unpack(struct hdmi_spd_infoframe * frame,void * buffer)1074 static int hdmi_spd_infoframe_unpack(struct hdmi_spd_infoframe *frame,
1075 				     void *buffer)
1076 {
1077 	u8 *ptr = buffer;
1078 	int ret;
1079 
1080 	if (ptr[0] != HDMI_INFOFRAME_TYPE_SPD ||
1081 	    ptr[1] != 1 ||
1082 	    ptr[2] != HDMI_SPD_INFOFRAME_SIZE) {
1083 		return -EINVAL;
1084 	}
1085 
1086 	if (hdmi_infoframe_checksum(buffer, HDMI_INFOFRAME_SIZE(SPD)) != 0)
1087 		return -EINVAL;
1088 
1089 	ptr += HDMI_INFOFRAME_HEADER_SIZE;
1090 
1091 	ret = hdmi_spd_infoframe_init(frame, ptr, ptr + 8);
1092 	if (ret)
1093 		return ret;
1094 
1095 	frame->sdi = ptr[24];
1096 
1097 	return 0;
1098 }
1099 
1100 /**
1101  * hdmi_audio_infoframe_unpack() - unpack binary buffer to a HDMI AUDIO infoframe
1102  * @buffer: source buffer
1103  * @frame: HDMI Audio infoframe
1104  *
1105  * Unpacks the information contained in binary @buffer into a structured
1106  * @frame of the HDMI Audio information frame.
1107  * Also verifies the checksum as required by section 5.3.5 of the HDMI 1.4
1108  * specification.
1109  *
1110  * Returns 0 on success or a negative error code on failure.
1111  */
hdmi_audio_infoframe_unpack(struct hdmi_audio_infoframe * frame,void * buffer)1112 static int hdmi_audio_infoframe_unpack(struct hdmi_audio_infoframe *frame,
1113 				       void *buffer)
1114 {
1115 	u8 *ptr = buffer;
1116 	int ret;
1117 
1118 	if (ptr[0] != HDMI_INFOFRAME_TYPE_AUDIO ||
1119 	    ptr[1] != 1 ||
1120 	    ptr[2] != HDMI_AUDIO_INFOFRAME_SIZE) {
1121 		return -EINVAL;
1122 	}
1123 
1124 	if (hdmi_infoframe_checksum(buffer, HDMI_INFOFRAME_SIZE(AUDIO)) != 0)
1125 		return -EINVAL;
1126 
1127 	ret = hdmi_audio_infoframe_init(frame);
1128 	if (ret)
1129 		return ret;
1130 
1131 	ptr += HDMI_INFOFRAME_HEADER_SIZE;
1132 
1133 	frame->channels = ptr[0] & 0x7;
1134 	frame->coding_type = (ptr[0] >> 4) & 0xf;
1135 	frame->sample_size = ptr[1] & 0x3;
1136 	frame->sample_frequency = (ptr[1] >> 2) & 0x7;
1137 	frame->coding_type_ext = ptr[2] & 0x1f;
1138 	frame->channel_allocation = ptr[3];
1139 	frame->level_shift_value = (ptr[4] >> 3) & 0xf;
1140 	frame->downmix_inhibit = ptr[4] & 0x80 ? true : false;
1141 
1142 	return 0;
1143 }
1144 
1145 /**
1146  * hdmi_vendor_infoframe_unpack() - unpack binary buffer to a HDMI vendor infoframe
1147  * @buffer: source buffer
1148  * @frame: HDMI Vendor infoframe
1149  *
1150  * Unpacks the information contained in binary @buffer into a structured
1151  * @frame of the HDMI Vendor information frame.
1152  * Also verifies the checksum as required by section 5.3.5 of the HDMI 1.4
1153  * specification.
1154  *
1155  * Returns 0 on success or a negative error code on failure.
1156  */
1157 static int
hdmi_vendor_any_infoframe_unpack(union hdmi_vendor_any_infoframe * frame,void * buffer)1158 hdmi_vendor_any_infoframe_unpack(union hdmi_vendor_any_infoframe *frame,
1159 				 void *buffer)
1160 {
1161 	u8 *ptr = buffer;
1162 	size_t length;
1163 	int ret;
1164 	u8 hdmi_video_format;
1165 	struct hdmi_vendor_infoframe *hvf = &frame->hdmi;
1166 
1167 	if (ptr[0] != HDMI_INFOFRAME_TYPE_VENDOR ||
1168 	    ptr[1] != 1 ||
1169 	    (ptr[2] != 4 && ptr[2] != 5 && ptr[2] != 6))
1170 		return -EINVAL;
1171 
1172 	length = ptr[2];
1173 
1174 	if (hdmi_infoframe_checksum(buffer,
1175 				    HDMI_INFOFRAME_HEADER_SIZE + length) != 0)
1176 		return -EINVAL;
1177 
1178 	ptr += HDMI_INFOFRAME_HEADER_SIZE;
1179 
1180 	/* HDMI OUI */
1181 	if ((ptr[0] != 0x03) ||
1182 	    (ptr[1] != 0x0c) ||
1183 	    (ptr[2] != 0x00))
1184 		return -EINVAL;
1185 
1186 	hdmi_video_format = ptr[3] >> 5;
1187 
1188 	if (hdmi_video_format > 0x2)
1189 		return -EINVAL;
1190 
1191 	ret = hdmi_vendor_infoframe_init(hvf);
1192 	if (ret)
1193 		return ret;
1194 
1195 	hvf->length = length;
1196 
1197 	if (hdmi_video_format == 0x2) {
1198 		if (length != 5 && length != 6)
1199 			return -EINVAL;
1200 		hvf->s3d_struct = ptr[4] >> 4;
1201 		if (hvf->s3d_struct >= HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF) {
1202 			if (length != 6)
1203 				return -EINVAL;
1204 			hvf->s3d_ext_data = ptr[5] >> 4;
1205 		}
1206 	} else if (hdmi_video_format == 0x1) {
1207 		if (length != 5)
1208 			return -EINVAL;
1209 		hvf->vic = ptr[4];
1210 	} else {
1211 		if (length != 4)
1212 			return -EINVAL;
1213 	}
1214 
1215 	return 0;
1216 }
1217 
1218 /**
1219  * hdmi_infoframe_unpack() - unpack binary buffer to a HDMI infoframe
1220  * @buffer: source buffer
1221  * @frame: HDMI infoframe
1222  *
1223  * Unpacks the information contained in binary buffer @buffer into a structured
1224  * @frame of a HDMI infoframe.
1225  * Also verifies the checksum as required by section 5.3.5 of the HDMI 1.4
1226  * specification.
1227  *
1228  * Returns 0 on success or a negative error code on failure.
1229  */
hdmi_infoframe_unpack(union hdmi_infoframe * frame,void * buffer)1230 int hdmi_infoframe_unpack(union hdmi_infoframe *frame, void *buffer)
1231 {
1232 	int ret;
1233 	u8 *ptr = buffer;
1234 
1235 	switch (ptr[0]) {
1236 	case HDMI_INFOFRAME_TYPE_AVI:
1237 		ret = hdmi_avi_infoframe_unpack(&frame->avi, buffer);
1238 		break;
1239 	case HDMI_INFOFRAME_TYPE_SPD:
1240 		ret = hdmi_spd_infoframe_unpack(&frame->spd, buffer);
1241 		break;
1242 	case HDMI_INFOFRAME_TYPE_AUDIO:
1243 		ret = hdmi_audio_infoframe_unpack(&frame->audio, buffer);
1244 		break;
1245 	case HDMI_INFOFRAME_TYPE_VENDOR:
1246 		ret = hdmi_vendor_any_infoframe_unpack(&frame->vendor, buffer);
1247 		break;
1248 	default:
1249 		ret = -EINVAL;
1250 		break;
1251 	}
1252 
1253 	return ret;
1254 }
1255 EXPORT_SYMBOL(hdmi_infoframe_unpack);
1256