xref: /openbmc/linux/drivers/video/hdmi.c (revision bb84bc51)
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 
34 static u8 hdmi_infoframe_checksum(const 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 
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 void hdmi_avi_infoframe_init(struct hdmi_avi_infoframe *frame)
58 {
59 	memset(frame, 0, sizeof(*frame));
60 
61 	frame->type = HDMI_INFOFRAME_TYPE_AVI;
62 	frame->version = 2;
63 	frame->length = HDMI_AVI_INFOFRAME_SIZE;
64 }
65 EXPORT_SYMBOL(hdmi_avi_infoframe_init);
66 
67 static int hdmi_avi_infoframe_check_only(const struct hdmi_avi_infoframe *frame)
68 {
69 	if (frame->type != HDMI_INFOFRAME_TYPE_AVI ||
70 	    frame->version != 2 ||
71 	    frame->length != HDMI_AVI_INFOFRAME_SIZE)
72 		return -EINVAL;
73 
74 	if (frame->picture_aspect > HDMI_PICTURE_ASPECT_16_9)
75 		return -EINVAL;
76 
77 	return 0;
78 }
79 
80 /**
81  * hdmi_avi_infoframe_check() - check a HDMI AVI infoframe
82  * @frame: HDMI AVI infoframe
83  *
84  * Validates that the infoframe is consistent and updates derived fields
85  * (eg. length) based on other fields.
86  *
87  * Returns 0 on success or a negative error code on failure.
88  */
89 int hdmi_avi_infoframe_check(struct hdmi_avi_infoframe *frame)
90 {
91 	return hdmi_avi_infoframe_check_only(frame);
92 }
93 EXPORT_SYMBOL(hdmi_avi_infoframe_check);
94 
95 /**
96  * hdmi_avi_infoframe_pack_only() - write HDMI AVI infoframe to binary buffer
97  * @frame: HDMI AVI infoframe
98  * @buffer: destination buffer
99  * @size: size of buffer
100  *
101  * Packs the information contained in the @frame structure into a binary
102  * representation that can be written into the corresponding controller
103  * registers. Also computes the checksum as required by section 5.3.5 of
104  * the HDMI 1.4 specification.
105  *
106  * Returns the number of bytes packed into the binary buffer or a negative
107  * error code on failure.
108  */
109 ssize_t hdmi_avi_infoframe_pack_only(const struct hdmi_avi_infoframe *frame,
110 				     void *buffer, size_t size)
111 {
112 	u8 *ptr = buffer;
113 	size_t length;
114 	int ret;
115 
116 	ret = hdmi_avi_infoframe_check_only(frame);
117 	if (ret)
118 		return ret;
119 
120 	length = HDMI_INFOFRAME_HEADER_SIZE + frame->length;
121 
122 	if (size < length)
123 		return -ENOSPC;
124 
125 	memset(buffer, 0, size);
126 
127 	ptr[0] = frame->type;
128 	ptr[1] = frame->version;
129 	ptr[2] = frame->length;
130 	ptr[3] = 0; /* checksum */
131 
132 	/* start infoframe payload */
133 	ptr += HDMI_INFOFRAME_HEADER_SIZE;
134 
135 	ptr[0] = ((frame->colorspace & 0x3) << 5) | (frame->scan_mode & 0x3);
136 
137 	/*
138 	 * Data byte 1, bit 4 has to be set if we provide the active format
139 	 * aspect ratio
140 	 */
141 	if (frame->active_aspect & 0xf)
142 		ptr[0] |= BIT(4);
143 
144 	/* Bit 3 and 2 indicate if we transmit horizontal/vertical bar data */
145 	if (frame->top_bar || frame->bottom_bar)
146 		ptr[0] |= BIT(3);
147 
148 	if (frame->left_bar || frame->right_bar)
149 		ptr[0] |= BIT(2);
150 
151 	ptr[1] = ((frame->colorimetry & 0x3) << 6) |
152 		 ((frame->picture_aspect & 0x3) << 4) |
153 		 (frame->active_aspect & 0xf);
154 
155 	ptr[2] = ((frame->extended_colorimetry & 0x7) << 4) |
156 		 ((frame->quantization_range & 0x3) << 2) |
157 		 (frame->nups & 0x3);
158 
159 	if (frame->itc)
160 		ptr[2] |= BIT(7);
161 
162 	ptr[3] = frame->video_code & 0x7f;
163 
164 	ptr[4] = ((frame->ycc_quantization_range & 0x3) << 6) |
165 		 ((frame->content_type & 0x3) << 4) |
166 		 (frame->pixel_repeat & 0xf);
167 
168 	ptr[5] = frame->top_bar & 0xff;
169 	ptr[6] = (frame->top_bar >> 8) & 0xff;
170 	ptr[7] = frame->bottom_bar & 0xff;
171 	ptr[8] = (frame->bottom_bar >> 8) & 0xff;
172 	ptr[9] = frame->left_bar & 0xff;
173 	ptr[10] = (frame->left_bar >> 8) & 0xff;
174 	ptr[11] = frame->right_bar & 0xff;
175 	ptr[12] = (frame->right_bar >> 8) & 0xff;
176 
177 	hdmi_infoframe_set_checksum(buffer, length);
178 
179 	return length;
180 }
181 EXPORT_SYMBOL(hdmi_avi_infoframe_pack_only);
182 
183 /**
184  * hdmi_avi_infoframe_pack() - check a HDMI AVI infoframe,
185  *                             and write it to binary buffer
186  * @frame: HDMI AVI infoframe
187  * @buffer: destination buffer
188  * @size: size of buffer
189  *
190  * Validates that the infoframe is consistent and updates derived fields
191  * (eg. length) based on other fields, after which it packs the information
192  * contained in the @frame structure into a binary representation that
193  * can be written into the corresponding controller registers. This function
194  * also computes the checksum as required by section 5.3.5 of the HDMI 1.4
195  * specification.
196  *
197  * Returns the number of bytes packed into the binary buffer or a negative
198  * error code on failure.
199  */
200 ssize_t hdmi_avi_infoframe_pack(struct hdmi_avi_infoframe *frame,
201 				void *buffer, size_t size)
202 {
203 	int ret;
204 
205 	ret = hdmi_avi_infoframe_check(frame);
206 	if (ret)
207 		return ret;
208 
209 	return hdmi_avi_infoframe_pack_only(frame, buffer, size);
210 }
211 EXPORT_SYMBOL(hdmi_avi_infoframe_pack);
212 
213 /**
214  * hdmi_spd_infoframe_init() - initialize an HDMI SPD infoframe
215  * @frame: HDMI SPD infoframe
216  * @vendor: vendor string
217  * @product: product string
218  *
219  * Returns 0 on success or a negative error code on failure.
220  */
221 int hdmi_spd_infoframe_init(struct hdmi_spd_infoframe *frame,
222 			    const char *vendor, const char *product)
223 {
224 	memset(frame, 0, sizeof(*frame));
225 
226 	frame->type = HDMI_INFOFRAME_TYPE_SPD;
227 	frame->version = 1;
228 	frame->length = HDMI_SPD_INFOFRAME_SIZE;
229 
230 	strncpy(frame->vendor, vendor, sizeof(frame->vendor));
231 	strncpy(frame->product, product, sizeof(frame->product));
232 
233 	return 0;
234 }
235 EXPORT_SYMBOL(hdmi_spd_infoframe_init);
236 
237 static int hdmi_spd_infoframe_check_only(const struct hdmi_spd_infoframe *frame)
238 {
239 	if (frame->type != HDMI_INFOFRAME_TYPE_SPD ||
240 	    frame->version != 1 ||
241 	    frame->length != HDMI_SPD_INFOFRAME_SIZE)
242 		return -EINVAL;
243 
244 	return 0;
245 }
246 
247 /**
248  * hdmi_spd_infoframe_check() - check a HDMI SPD infoframe
249  * @frame: HDMI SPD infoframe
250  *
251  * Validates that the infoframe is consistent and updates derived fields
252  * (eg. length) based on other fields.
253  *
254  * Returns 0 on success or a negative error code on failure.
255  */
256 int hdmi_spd_infoframe_check(struct hdmi_spd_infoframe *frame)
257 {
258 	return hdmi_spd_infoframe_check_only(frame);
259 }
260 EXPORT_SYMBOL(hdmi_spd_infoframe_check);
261 
262 /**
263  * hdmi_spd_infoframe_pack_only() - write HDMI SPD infoframe to binary buffer
264  * @frame: HDMI SPD infoframe
265  * @buffer: destination buffer
266  * @size: size of buffer
267  *
268  * Packs the information contained in the @frame structure into a binary
269  * representation that can be written into the corresponding controller
270  * registers. Also computes the checksum as required by section 5.3.5 of
271  * the HDMI 1.4 specification.
272  *
273  * Returns the number of bytes packed into the binary buffer or a negative
274  * error code on failure.
275  */
276 ssize_t hdmi_spd_infoframe_pack_only(const struct hdmi_spd_infoframe *frame,
277 				     void *buffer, size_t size)
278 {
279 	u8 *ptr = buffer;
280 	size_t length;
281 	int ret;
282 
283 	ret = hdmi_spd_infoframe_check_only(frame);
284 	if (ret)
285 		return ret;
286 
287 	length = HDMI_INFOFRAME_HEADER_SIZE + frame->length;
288 
289 	if (size < length)
290 		return -ENOSPC;
291 
292 	memset(buffer, 0, size);
293 
294 	ptr[0] = frame->type;
295 	ptr[1] = frame->version;
296 	ptr[2] = frame->length;
297 	ptr[3] = 0; /* checksum */
298 
299 	/* start infoframe payload */
300 	ptr += HDMI_INFOFRAME_HEADER_SIZE;
301 
302 	memcpy(ptr, frame->vendor, sizeof(frame->vendor));
303 	memcpy(ptr + 8, frame->product, sizeof(frame->product));
304 
305 	ptr[24] = frame->sdi;
306 
307 	hdmi_infoframe_set_checksum(buffer, length);
308 
309 	return length;
310 }
311 EXPORT_SYMBOL(hdmi_spd_infoframe_pack_only);
312 
313 /**
314  * hdmi_spd_infoframe_pack() - check a HDMI SPD infoframe,
315  *                             and write it to binary buffer
316  * @frame: HDMI SPD infoframe
317  * @buffer: destination buffer
318  * @size: size of buffer
319  *
320  * Validates that the infoframe is consistent and updates derived fields
321  * (eg. length) based on other fields, after which it packs the information
322  * contained in the @frame structure into a binary representation that
323  * can be written into the corresponding controller registers. This function
324  * also computes the checksum as required by section 5.3.5 of the HDMI 1.4
325  * specification.
326  *
327  * Returns the number of bytes packed into the binary buffer or a negative
328  * error code on failure.
329  */
330 ssize_t hdmi_spd_infoframe_pack(struct hdmi_spd_infoframe *frame,
331 				void *buffer, size_t size)
332 {
333 	int ret;
334 
335 	ret = hdmi_spd_infoframe_check(frame);
336 	if (ret)
337 		return ret;
338 
339 	return hdmi_spd_infoframe_pack_only(frame, buffer, size);
340 }
341 EXPORT_SYMBOL(hdmi_spd_infoframe_pack);
342 
343 /**
344  * hdmi_audio_infoframe_init() - initialize an HDMI audio infoframe
345  * @frame: HDMI audio infoframe
346  *
347  * Returns 0 on success or a negative error code on failure.
348  */
349 int hdmi_audio_infoframe_init(struct hdmi_audio_infoframe *frame)
350 {
351 	memset(frame, 0, sizeof(*frame));
352 
353 	frame->type = HDMI_INFOFRAME_TYPE_AUDIO;
354 	frame->version = 1;
355 	frame->length = HDMI_AUDIO_INFOFRAME_SIZE;
356 
357 	return 0;
358 }
359 EXPORT_SYMBOL(hdmi_audio_infoframe_init);
360 
361 static int hdmi_audio_infoframe_check_only(const struct hdmi_audio_infoframe *frame)
362 {
363 	if (frame->type != HDMI_INFOFRAME_TYPE_AUDIO ||
364 	    frame->version != 1 ||
365 	    frame->length != HDMI_AUDIO_INFOFRAME_SIZE)
366 		return -EINVAL;
367 
368 	return 0;
369 }
370 
371 /**
372  * hdmi_audio_infoframe_check() - check a HDMI audio infoframe
373  * @frame: HDMI audio infoframe
374  *
375  * Validates that the infoframe is consistent and updates derived fields
376  * (eg. length) based on other fields.
377  *
378  * Returns 0 on success or a negative error code on failure.
379  */
380 int hdmi_audio_infoframe_check(struct hdmi_audio_infoframe *frame)
381 {
382 	return hdmi_audio_infoframe_check_only(frame);
383 }
384 EXPORT_SYMBOL(hdmi_audio_infoframe_check);
385 
386 /**
387  * hdmi_audio_infoframe_pack_only() - write HDMI audio infoframe to binary buffer
388  * @frame: HDMI audio infoframe
389  * @buffer: destination buffer
390  * @size: size of buffer
391  *
392  * Packs the information contained in the @frame structure into a binary
393  * representation that can be written into the corresponding controller
394  * registers. Also computes the checksum as required by section 5.3.5 of
395  * the HDMI 1.4 specification.
396  *
397  * Returns the number of bytes packed into the binary buffer or a negative
398  * error code on failure.
399  */
400 ssize_t hdmi_audio_infoframe_pack_only(const struct hdmi_audio_infoframe *frame,
401 				       void *buffer, size_t size)
402 {
403 	unsigned char channels;
404 	u8 *ptr = buffer;
405 	size_t length;
406 	int ret;
407 
408 	ret = hdmi_audio_infoframe_check_only(frame);
409 	if (ret)
410 		return ret;
411 
412 	length = HDMI_INFOFRAME_HEADER_SIZE + frame->length;
413 
414 	if (size < length)
415 		return -ENOSPC;
416 
417 	memset(buffer, 0, size);
418 
419 	if (frame->channels >= 2)
420 		channels = frame->channels - 1;
421 	else
422 		channels = 0;
423 
424 	ptr[0] = frame->type;
425 	ptr[1] = frame->version;
426 	ptr[2] = frame->length;
427 	ptr[3] = 0; /* checksum */
428 
429 	/* start infoframe payload */
430 	ptr += HDMI_INFOFRAME_HEADER_SIZE;
431 
432 	ptr[0] = ((frame->coding_type & 0xf) << 4) | (channels & 0x7);
433 	ptr[1] = ((frame->sample_frequency & 0x7) << 2) |
434 		 (frame->sample_size & 0x3);
435 	ptr[2] = frame->coding_type_ext & 0x1f;
436 	ptr[3] = frame->channel_allocation;
437 	ptr[4] = (frame->level_shift_value & 0xf) << 3;
438 
439 	if (frame->downmix_inhibit)
440 		ptr[4] |= BIT(7);
441 
442 	hdmi_infoframe_set_checksum(buffer, length);
443 
444 	return length;
445 }
446 EXPORT_SYMBOL(hdmi_audio_infoframe_pack_only);
447 
448 /**
449  * hdmi_audio_infoframe_pack() - check a HDMI Audio infoframe,
450  *                               and write it to binary buffer
451  * @frame: HDMI Audio infoframe
452  * @buffer: destination buffer
453  * @size: size of buffer
454  *
455  * Validates that the infoframe is consistent and updates derived fields
456  * (eg. length) based on other fields, after which it packs the information
457  * contained in the @frame structure into a binary representation that
458  * can be written into the corresponding controller registers. This function
459  * also computes the checksum as required by section 5.3.5 of the HDMI 1.4
460  * specification.
461  *
462  * Returns the number of bytes packed into the binary buffer or a negative
463  * error code on failure.
464  */
465 ssize_t hdmi_audio_infoframe_pack(struct hdmi_audio_infoframe *frame,
466 				  void *buffer, size_t size)
467 {
468 	int ret;
469 
470 	ret = hdmi_audio_infoframe_check(frame);
471 	if (ret)
472 		return ret;
473 
474 	return hdmi_audio_infoframe_pack_only(frame, buffer, size);
475 }
476 EXPORT_SYMBOL(hdmi_audio_infoframe_pack);
477 
478 /**
479  * hdmi_vendor_infoframe_init() - initialize an HDMI vendor infoframe
480  * @frame: HDMI vendor infoframe
481  *
482  * Returns 0 on success or a negative error code on failure.
483  */
484 int hdmi_vendor_infoframe_init(struct hdmi_vendor_infoframe *frame)
485 {
486 	memset(frame, 0, sizeof(*frame));
487 
488 	frame->type = HDMI_INFOFRAME_TYPE_VENDOR;
489 	frame->version = 1;
490 
491 	frame->oui = HDMI_IEEE_OUI;
492 
493 	/*
494 	 * 0 is a valid value for s3d_struct, so we use a special "not set"
495 	 * value
496 	 */
497 	frame->s3d_struct = HDMI_3D_STRUCTURE_INVALID;
498 	frame->length = HDMI_VENDOR_INFOFRAME_SIZE;
499 
500 	return 0;
501 }
502 EXPORT_SYMBOL(hdmi_vendor_infoframe_init);
503 
504 static int hdmi_vendor_infoframe_length(const struct hdmi_vendor_infoframe *frame)
505 {
506 	/* for side by side (half) we also need to provide 3D_Ext_Data */
507 	if (frame->s3d_struct >= HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF)
508 		return 6;
509 	else if (frame->vic != 0 || frame->s3d_struct != HDMI_3D_STRUCTURE_INVALID)
510 		return 5;
511 	else
512 		return 4;
513 }
514 
515 static int hdmi_vendor_infoframe_check_only(const struct hdmi_vendor_infoframe *frame)
516 {
517 	if (frame->type != HDMI_INFOFRAME_TYPE_VENDOR ||
518 	    frame->version != 1 ||
519 	    frame->oui != HDMI_IEEE_OUI)
520 		return -EINVAL;
521 
522 	/* only one of those can be supplied */
523 	if (frame->vic != 0 && frame->s3d_struct != HDMI_3D_STRUCTURE_INVALID)
524 		return -EINVAL;
525 
526 	if (frame->length != hdmi_vendor_infoframe_length(frame))
527 		return -EINVAL;
528 
529 	return 0;
530 }
531 
532 /**
533  * hdmi_vendor_infoframe_check() - check a HDMI vendor infoframe
534  * @frame: HDMI infoframe
535  *
536  * Validates that the infoframe is consistent and updates derived fields
537  * (eg. length) based on other fields.
538  *
539  * Returns 0 on success or a negative error code on failure.
540  */
541 int hdmi_vendor_infoframe_check(struct hdmi_vendor_infoframe *frame)
542 {
543 	frame->length = hdmi_vendor_infoframe_length(frame);
544 
545 	return hdmi_vendor_infoframe_check_only(frame);
546 }
547 EXPORT_SYMBOL(hdmi_vendor_infoframe_check);
548 
549 /**
550  * hdmi_vendor_infoframe_pack_only() - write a HDMI vendor infoframe to binary buffer
551  * @frame: HDMI infoframe
552  * @buffer: destination buffer
553  * @size: size of buffer
554  *
555  * Packs the information contained in the @frame structure into a binary
556  * representation that can be written into the corresponding controller
557  * registers. Also computes the checksum as required by section 5.3.5 of
558  * the HDMI 1.4 specification.
559  *
560  * Returns the number of bytes packed into the binary buffer or a negative
561  * error code on failure.
562  */
563 ssize_t hdmi_vendor_infoframe_pack_only(const struct hdmi_vendor_infoframe *frame,
564 					void *buffer, size_t size)
565 {
566 	u8 *ptr = buffer;
567 	size_t length;
568 	int ret;
569 
570 	ret = hdmi_vendor_infoframe_check_only(frame);
571 	if (ret)
572 		return ret;
573 
574 	length = HDMI_INFOFRAME_HEADER_SIZE + frame->length;
575 
576 	if (size < length)
577 		return -ENOSPC;
578 
579 	memset(buffer, 0, size);
580 
581 	ptr[0] = frame->type;
582 	ptr[1] = frame->version;
583 	ptr[2] = frame->length;
584 	ptr[3] = 0; /* checksum */
585 
586 	/* HDMI OUI */
587 	ptr[4] = 0x03;
588 	ptr[5] = 0x0c;
589 	ptr[6] = 0x00;
590 
591 	if (frame->s3d_struct != HDMI_3D_STRUCTURE_INVALID) {
592 		ptr[7] = 0x2 << 5;	/* video format */
593 		ptr[8] = (frame->s3d_struct & 0xf) << 4;
594 		if (frame->s3d_struct >= HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF)
595 			ptr[9] = (frame->s3d_ext_data & 0xf) << 4;
596 	} else if (frame->vic) {
597 		ptr[7] = 0x1 << 5;	/* video format */
598 		ptr[8] = frame->vic;
599 	} else {
600 		ptr[7] = 0x0 << 5;	/* video format */
601 	}
602 
603 	hdmi_infoframe_set_checksum(buffer, length);
604 
605 	return length;
606 }
607 EXPORT_SYMBOL(hdmi_vendor_infoframe_pack_only);
608 
609 /**
610  * hdmi_vendor_infoframe_pack() - check a HDMI Vendor infoframe,
611  *                                and write it to binary buffer
612  * @frame: HDMI Vendor infoframe
613  * @buffer: destination buffer
614  * @size: size of buffer
615  *
616  * Validates that the infoframe is consistent and updates derived fields
617  * (eg. length) based on other fields, after which it packs the information
618  * contained in the @frame structure into a binary representation that
619  * can be written into the corresponding controller registers. This function
620  * also computes the checksum as required by section 5.3.5 of the HDMI 1.4
621  * specification.
622  *
623  * Returns the number of bytes packed into the binary buffer or a negative
624  * error code on failure.
625  */
626 ssize_t hdmi_vendor_infoframe_pack(struct hdmi_vendor_infoframe *frame,
627 				   void *buffer, size_t size)
628 {
629 	int ret;
630 
631 	ret = hdmi_vendor_infoframe_check(frame);
632 	if (ret)
633 		return ret;
634 
635 	return hdmi_vendor_infoframe_pack_only(frame, buffer, size);
636 }
637 EXPORT_SYMBOL(hdmi_vendor_infoframe_pack);
638 
639 static int
640 hdmi_vendor_any_infoframe_check_only(const union hdmi_vendor_any_infoframe *frame)
641 {
642 	if (frame->any.type != HDMI_INFOFRAME_TYPE_VENDOR ||
643 	    frame->any.version != 1)
644 		return -EINVAL;
645 
646 	return 0;
647 }
648 
649 /**
650  * hdmi_drm_infoframe_init() - initialize an HDMI Dynaminc Range and
651  * mastering infoframe
652  * @frame: HDMI DRM infoframe
653  *
654  * Returns 0 on success or a negative error code on failure.
655  */
656 int hdmi_drm_infoframe_init(struct hdmi_drm_infoframe *frame)
657 {
658 	memset(frame, 0, sizeof(*frame));
659 
660 	frame->type = HDMI_INFOFRAME_TYPE_DRM;
661 	frame->version = 1;
662 	frame->length = HDMI_DRM_INFOFRAME_SIZE;
663 
664 	return 0;
665 }
666 EXPORT_SYMBOL(hdmi_drm_infoframe_init);
667 
668 static int hdmi_drm_infoframe_check_only(const struct hdmi_drm_infoframe *frame)
669 {
670 	if (frame->type != HDMI_INFOFRAME_TYPE_DRM ||
671 	    frame->version != 1)
672 		return -EINVAL;
673 
674 	if (frame->length != HDMI_DRM_INFOFRAME_SIZE)
675 		return -EINVAL;
676 
677 	return 0;
678 }
679 
680 /**
681  * hdmi_drm_infoframe_check() - check a HDMI DRM infoframe
682  * @frame: HDMI DRM infoframe
683  *
684  * Validates that the infoframe is consistent.
685  * Returns 0 on success or a negative error code on failure.
686  */
687 int hdmi_drm_infoframe_check(struct hdmi_drm_infoframe *frame)
688 {
689 	return hdmi_drm_infoframe_check_only(frame);
690 }
691 EXPORT_SYMBOL(hdmi_drm_infoframe_check);
692 
693 /**
694  * hdmi_drm_infoframe_pack_only() - write HDMI DRM infoframe to binary buffer
695  * @frame: HDMI DRM infoframe
696  * @buffer: destination buffer
697  * @size: size of buffer
698  *
699  * Packs the information contained in the @frame structure into a binary
700  * representation that can be written into the corresponding controller
701  * registers. Also computes the checksum as required by section 5.3.5 of
702  * the HDMI 1.4 specification.
703  *
704  * Returns the number of bytes packed into the binary buffer or a negative
705  * error code on failure.
706  */
707 ssize_t hdmi_drm_infoframe_pack_only(const struct hdmi_drm_infoframe *frame,
708 				     void *buffer, size_t size)
709 {
710 	u8 *ptr = buffer;
711 	size_t length;
712 	int i;
713 
714 	length = HDMI_INFOFRAME_HEADER_SIZE + frame->length;
715 
716 	if (size < length)
717 		return -ENOSPC;
718 
719 	memset(buffer, 0, size);
720 
721 	ptr[0] = frame->type;
722 	ptr[1] = frame->version;
723 	ptr[2] = frame->length;
724 	ptr[3] = 0; /* checksum */
725 
726 	/* start infoframe payload */
727 	ptr += HDMI_INFOFRAME_HEADER_SIZE;
728 
729 	*ptr++ = frame->eotf;
730 	*ptr++ = frame->metadata_type;
731 
732 	for (i = 0; i < 3; i++) {
733 		*ptr++ = frame->display_primaries[i].x;
734 		*ptr++ = frame->display_primaries[i].x >> 8;
735 		*ptr++ = frame->display_primaries[i].y;
736 		*ptr++ = frame->display_primaries[i].y >> 8;
737 	}
738 
739 	*ptr++ = frame->white_point.x;
740 	*ptr++ = frame->white_point.x >> 8;
741 
742 	*ptr++ = frame->white_point.y;
743 	*ptr++ = frame->white_point.y >> 8;
744 
745 	*ptr++ = frame->max_display_mastering_luminance;
746 	*ptr++ = frame->max_display_mastering_luminance >> 8;
747 
748 	*ptr++ = frame->min_display_mastering_luminance;
749 	*ptr++ = frame->min_display_mastering_luminance >> 8;
750 
751 	*ptr++ = frame->max_cll;
752 	*ptr++ = frame->max_cll >> 8;
753 
754 	*ptr++ = frame->max_fall;
755 	*ptr++ = frame->max_fall >> 8;
756 
757 	hdmi_infoframe_set_checksum(buffer, length);
758 
759 	return length;
760 }
761 EXPORT_SYMBOL(hdmi_drm_infoframe_pack_only);
762 
763 /**
764  * hdmi_drm_infoframe_pack() - check a HDMI DRM infoframe,
765  *                             and write it to binary buffer
766  * @frame: HDMI DRM infoframe
767  * @buffer: destination buffer
768  * @size: size of buffer
769  *
770  * Validates that the infoframe is consistent and updates derived fields
771  * (eg. length) based on other fields, after which it packs the information
772  * contained in the @frame structure into a binary representation that
773  * can be written into the corresponding controller registers. This function
774  * also computes the checksum as required by section 5.3.5 of the HDMI 1.4
775  * specification.
776  *
777  * Returns the number of bytes packed into the binary buffer or a negative
778  * error code on failure.
779  */
780 ssize_t hdmi_drm_infoframe_pack(struct hdmi_drm_infoframe *frame,
781 				void *buffer, size_t size)
782 {
783 	int ret;
784 
785 	ret = hdmi_drm_infoframe_check(frame);
786 	if (ret)
787 		return ret;
788 
789 	return hdmi_drm_infoframe_pack_only(frame, buffer, size);
790 }
791 EXPORT_SYMBOL(hdmi_drm_infoframe_pack);
792 
793 /*
794  * hdmi_vendor_any_infoframe_check() - check a vendor infoframe
795  */
796 static int
797 hdmi_vendor_any_infoframe_check(union hdmi_vendor_any_infoframe *frame)
798 {
799 	int ret;
800 
801 	ret = hdmi_vendor_any_infoframe_check_only(frame);
802 	if (ret)
803 		return ret;
804 
805 	/* we only know about HDMI vendor infoframes */
806 	if (frame->any.oui != HDMI_IEEE_OUI)
807 		return -EINVAL;
808 
809 	return hdmi_vendor_infoframe_check(&frame->hdmi);
810 }
811 
812 /*
813  * hdmi_vendor_any_infoframe_pack_only() - write a vendor infoframe to binary buffer
814  */
815 static ssize_t
816 hdmi_vendor_any_infoframe_pack_only(const union hdmi_vendor_any_infoframe *frame,
817 				    void *buffer, size_t size)
818 {
819 	int ret;
820 
821 	ret = hdmi_vendor_any_infoframe_check_only(frame);
822 	if (ret)
823 		return ret;
824 
825 	/* we only know about HDMI vendor infoframes */
826 	if (frame->any.oui != HDMI_IEEE_OUI)
827 		return -EINVAL;
828 
829 	return hdmi_vendor_infoframe_pack_only(&frame->hdmi, buffer, size);
830 }
831 
832 /*
833  * hdmi_vendor_any_infoframe_pack() - check a vendor infoframe,
834  *                                    and write it to binary buffer
835  */
836 static ssize_t
837 hdmi_vendor_any_infoframe_pack(union hdmi_vendor_any_infoframe *frame,
838 			       void *buffer, size_t size)
839 {
840 	int ret;
841 
842 	ret = hdmi_vendor_any_infoframe_check(frame);
843 	if (ret)
844 		return ret;
845 
846 	return hdmi_vendor_any_infoframe_pack_only(frame, buffer, size);
847 }
848 
849 /**
850  * hdmi_infoframe_check() - check a HDMI infoframe
851  * @frame: HDMI infoframe
852  *
853  * Validates that the infoframe is consistent and updates derived fields
854  * (eg. length) based on other fields.
855  *
856  * Returns 0 on success or a negative error code on failure.
857  */
858 int
859 hdmi_infoframe_check(union hdmi_infoframe *frame)
860 {
861 	switch (frame->any.type) {
862 	case HDMI_INFOFRAME_TYPE_AVI:
863 		return hdmi_avi_infoframe_check(&frame->avi);
864 	case HDMI_INFOFRAME_TYPE_SPD:
865 		return hdmi_spd_infoframe_check(&frame->spd);
866 	case HDMI_INFOFRAME_TYPE_AUDIO:
867 		return hdmi_audio_infoframe_check(&frame->audio);
868 	case HDMI_INFOFRAME_TYPE_VENDOR:
869 		return hdmi_vendor_any_infoframe_check(&frame->vendor);
870 	default:
871 		WARN(1, "Bad infoframe type %d\n", frame->any.type);
872 		return -EINVAL;
873 	}
874 }
875 EXPORT_SYMBOL(hdmi_infoframe_check);
876 
877 /**
878  * hdmi_infoframe_pack_only() - write a HDMI infoframe to binary buffer
879  * @frame: HDMI infoframe
880  * @buffer: destination buffer
881  * @size: size of buffer
882  *
883  * Packs the information contained in the @frame structure into a binary
884  * representation that can be written into the corresponding controller
885  * registers. Also computes the checksum as required by section 5.3.5 of
886  * the HDMI 1.4 specification.
887  *
888  * Returns the number of bytes packed into the binary buffer or a negative
889  * error code on failure.
890  */
891 ssize_t
892 hdmi_infoframe_pack_only(const union hdmi_infoframe *frame, void *buffer, size_t size)
893 {
894 	ssize_t length;
895 
896 	switch (frame->any.type) {
897 	case HDMI_INFOFRAME_TYPE_AVI:
898 		length = hdmi_avi_infoframe_pack_only(&frame->avi,
899 						      buffer, size);
900 		break;
901 	case HDMI_INFOFRAME_TYPE_DRM:
902 		length = hdmi_drm_infoframe_pack_only(&frame->drm,
903 						      buffer, size);
904 		break;
905 	case HDMI_INFOFRAME_TYPE_SPD:
906 		length = hdmi_spd_infoframe_pack_only(&frame->spd,
907 						      buffer, size);
908 		break;
909 	case HDMI_INFOFRAME_TYPE_AUDIO:
910 		length = hdmi_audio_infoframe_pack_only(&frame->audio,
911 							buffer, size);
912 		break;
913 	case HDMI_INFOFRAME_TYPE_VENDOR:
914 		length = hdmi_vendor_any_infoframe_pack_only(&frame->vendor,
915 							     buffer, size);
916 		break;
917 	default:
918 		WARN(1, "Bad infoframe type %d\n", frame->any.type);
919 		length = -EINVAL;
920 	}
921 
922 	return length;
923 }
924 EXPORT_SYMBOL(hdmi_infoframe_pack_only);
925 
926 /**
927  * hdmi_infoframe_pack() - check a HDMI infoframe,
928  *                         and write it to binary buffer
929  * @frame: HDMI infoframe
930  * @buffer: destination buffer
931  * @size: size of buffer
932  *
933  * Validates that the infoframe is consistent and updates derived fields
934  * (eg. length) based on other fields, after which it packs the information
935  * contained in the @frame structure into a binary representation that
936  * can be written into the corresponding controller registers. This function
937  * also computes the checksum as required by section 5.3.5 of the HDMI 1.4
938  * specification.
939  *
940  * Returns the number of bytes packed into the binary buffer or a negative
941  * error code on failure.
942  */
943 ssize_t
944 hdmi_infoframe_pack(union hdmi_infoframe *frame,
945 		    void *buffer, size_t size)
946 {
947 	ssize_t length;
948 
949 	switch (frame->any.type) {
950 	case HDMI_INFOFRAME_TYPE_AVI:
951 		length = hdmi_avi_infoframe_pack(&frame->avi, buffer, size);
952 		break;
953 	case HDMI_INFOFRAME_TYPE_DRM:
954 		length = hdmi_drm_infoframe_pack(&frame->drm, buffer, size);
955 		break;
956 	case HDMI_INFOFRAME_TYPE_SPD:
957 		length = hdmi_spd_infoframe_pack(&frame->spd, buffer, size);
958 		break;
959 	case HDMI_INFOFRAME_TYPE_AUDIO:
960 		length = hdmi_audio_infoframe_pack(&frame->audio, buffer, size);
961 		break;
962 	case HDMI_INFOFRAME_TYPE_VENDOR:
963 		length = hdmi_vendor_any_infoframe_pack(&frame->vendor,
964 							buffer, size);
965 		break;
966 	default:
967 		WARN(1, "Bad infoframe type %d\n", frame->any.type);
968 		length = -EINVAL;
969 	}
970 
971 	return length;
972 }
973 EXPORT_SYMBOL(hdmi_infoframe_pack);
974 
975 static const char *hdmi_infoframe_type_get_name(enum hdmi_infoframe_type type)
976 {
977 	if (type < 0x80 || type > 0x9f)
978 		return "Invalid";
979 	switch (type) {
980 	case HDMI_INFOFRAME_TYPE_VENDOR:
981 		return "Vendor";
982 	case HDMI_INFOFRAME_TYPE_AVI:
983 		return "Auxiliary Video Information (AVI)";
984 	case HDMI_INFOFRAME_TYPE_SPD:
985 		return "Source Product Description (SPD)";
986 	case HDMI_INFOFRAME_TYPE_AUDIO:
987 		return "Audio";
988 	case HDMI_INFOFRAME_TYPE_DRM:
989 		return "Dynamic Range and Mastering";
990 	}
991 	return "Reserved";
992 }
993 
994 static void hdmi_infoframe_log_header(const char *level,
995 				      struct device *dev,
996 				      const struct hdmi_any_infoframe *frame)
997 {
998 	hdmi_log("HDMI infoframe: %s, version %u, length %u\n",
999 		hdmi_infoframe_type_get_name(frame->type),
1000 		frame->version, frame->length);
1001 }
1002 
1003 static const char *hdmi_colorspace_get_name(enum hdmi_colorspace colorspace)
1004 {
1005 	switch (colorspace) {
1006 	case HDMI_COLORSPACE_RGB:
1007 		return "RGB";
1008 	case HDMI_COLORSPACE_YUV422:
1009 		return "YCbCr 4:2:2";
1010 	case HDMI_COLORSPACE_YUV444:
1011 		return "YCbCr 4:4:4";
1012 	case HDMI_COLORSPACE_YUV420:
1013 		return "YCbCr 4:2:0";
1014 	case HDMI_COLORSPACE_RESERVED4:
1015 		return "Reserved (4)";
1016 	case HDMI_COLORSPACE_RESERVED5:
1017 		return "Reserved (5)";
1018 	case HDMI_COLORSPACE_RESERVED6:
1019 		return "Reserved (6)";
1020 	case HDMI_COLORSPACE_IDO_DEFINED:
1021 		return "IDO Defined";
1022 	}
1023 	return "Invalid";
1024 }
1025 
1026 static const char *hdmi_scan_mode_get_name(enum hdmi_scan_mode scan_mode)
1027 {
1028 	switch (scan_mode) {
1029 	case HDMI_SCAN_MODE_NONE:
1030 		return "No Data";
1031 	case HDMI_SCAN_MODE_OVERSCAN:
1032 		return "Overscan";
1033 	case HDMI_SCAN_MODE_UNDERSCAN:
1034 		return "Underscan";
1035 	case HDMI_SCAN_MODE_RESERVED:
1036 		return "Reserved";
1037 	}
1038 	return "Invalid";
1039 }
1040 
1041 static const char *hdmi_colorimetry_get_name(enum hdmi_colorimetry colorimetry)
1042 {
1043 	switch (colorimetry) {
1044 	case HDMI_COLORIMETRY_NONE:
1045 		return "No Data";
1046 	case HDMI_COLORIMETRY_ITU_601:
1047 		return "ITU601";
1048 	case HDMI_COLORIMETRY_ITU_709:
1049 		return "ITU709";
1050 	case HDMI_COLORIMETRY_EXTENDED:
1051 		return "Extended";
1052 	}
1053 	return "Invalid";
1054 }
1055 
1056 static const char *
1057 hdmi_picture_aspect_get_name(enum hdmi_picture_aspect picture_aspect)
1058 {
1059 	switch (picture_aspect) {
1060 	case HDMI_PICTURE_ASPECT_NONE:
1061 		return "No Data";
1062 	case HDMI_PICTURE_ASPECT_4_3:
1063 		return "4:3";
1064 	case HDMI_PICTURE_ASPECT_16_9:
1065 		return "16:9";
1066 	case HDMI_PICTURE_ASPECT_64_27:
1067 		return "64:27";
1068 	case HDMI_PICTURE_ASPECT_256_135:
1069 		return "256:135";
1070 	case HDMI_PICTURE_ASPECT_RESERVED:
1071 		return "Reserved";
1072 	}
1073 	return "Invalid";
1074 }
1075 
1076 static const char *
1077 hdmi_active_aspect_get_name(enum hdmi_active_aspect active_aspect)
1078 {
1079 	if (active_aspect < 0 || active_aspect > 0xf)
1080 		return "Invalid";
1081 
1082 	switch (active_aspect) {
1083 	case HDMI_ACTIVE_ASPECT_16_9_TOP:
1084 		return "16:9 Top";
1085 	case HDMI_ACTIVE_ASPECT_14_9_TOP:
1086 		return "14:9 Top";
1087 	case HDMI_ACTIVE_ASPECT_16_9_CENTER:
1088 		return "16:9 Center";
1089 	case HDMI_ACTIVE_ASPECT_PICTURE:
1090 		return "Same as Picture";
1091 	case HDMI_ACTIVE_ASPECT_4_3:
1092 		return "4:3";
1093 	case HDMI_ACTIVE_ASPECT_16_9:
1094 		return "16:9";
1095 	case HDMI_ACTIVE_ASPECT_14_9:
1096 		return "14:9";
1097 	case HDMI_ACTIVE_ASPECT_4_3_SP_14_9:
1098 		return "4:3 SP 14:9";
1099 	case HDMI_ACTIVE_ASPECT_16_9_SP_14_9:
1100 		return "16:9 SP 14:9";
1101 	case HDMI_ACTIVE_ASPECT_16_9_SP_4_3:
1102 		return "16:9 SP 4:3";
1103 	}
1104 	return "Reserved";
1105 }
1106 
1107 static const char *
1108 hdmi_extended_colorimetry_get_name(enum hdmi_extended_colorimetry ext_col)
1109 {
1110 	switch (ext_col) {
1111 	case HDMI_EXTENDED_COLORIMETRY_XV_YCC_601:
1112 		return "xvYCC 601";
1113 	case HDMI_EXTENDED_COLORIMETRY_XV_YCC_709:
1114 		return "xvYCC 709";
1115 	case HDMI_EXTENDED_COLORIMETRY_S_YCC_601:
1116 		return "sYCC 601";
1117 	case HDMI_EXTENDED_COLORIMETRY_OPYCC_601:
1118 		return "opYCC 601";
1119 	case HDMI_EXTENDED_COLORIMETRY_OPRGB:
1120 		return "opRGB";
1121 	case HDMI_EXTENDED_COLORIMETRY_BT2020_CONST_LUM:
1122 		return "BT.2020 Constant Luminance";
1123 	case HDMI_EXTENDED_COLORIMETRY_BT2020:
1124 		return "BT.2020";
1125 	case HDMI_EXTENDED_COLORIMETRY_RESERVED:
1126 		return "Reserved";
1127 	}
1128 	return "Invalid";
1129 }
1130 
1131 static const char *
1132 hdmi_quantization_range_get_name(enum hdmi_quantization_range qrange)
1133 {
1134 	switch (qrange) {
1135 	case HDMI_QUANTIZATION_RANGE_DEFAULT:
1136 		return "Default";
1137 	case HDMI_QUANTIZATION_RANGE_LIMITED:
1138 		return "Limited";
1139 	case HDMI_QUANTIZATION_RANGE_FULL:
1140 		return "Full";
1141 	case HDMI_QUANTIZATION_RANGE_RESERVED:
1142 		return "Reserved";
1143 	}
1144 	return "Invalid";
1145 }
1146 
1147 static const char *hdmi_nups_get_name(enum hdmi_nups nups)
1148 {
1149 	switch (nups) {
1150 	case HDMI_NUPS_UNKNOWN:
1151 		return "Unknown Non-uniform Scaling";
1152 	case HDMI_NUPS_HORIZONTAL:
1153 		return "Horizontally Scaled";
1154 	case HDMI_NUPS_VERTICAL:
1155 		return "Vertically Scaled";
1156 	case HDMI_NUPS_BOTH:
1157 		return "Horizontally and Vertically Scaled";
1158 	}
1159 	return "Invalid";
1160 }
1161 
1162 static const char *
1163 hdmi_ycc_quantization_range_get_name(enum hdmi_ycc_quantization_range qrange)
1164 {
1165 	switch (qrange) {
1166 	case HDMI_YCC_QUANTIZATION_RANGE_LIMITED:
1167 		return "Limited";
1168 	case HDMI_YCC_QUANTIZATION_RANGE_FULL:
1169 		return "Full";
1170 	}
1171 	return "Invalid";
1172 }
1173 
1174 static const char *
1175 hdmi_content_type_get_name(enum hdmi_content_type content_type)
1176 {
1177 	switch (content_type) {
1178 	case HDMI_CONTENT_TYPE_GRAPHICS:
1179 		return "Graphics";
1180 	case HDMI_CONTENT_TYPE_PHOTO:
1181 		return "Photo";
1182 	case HDMI_CONTENT_TYPE_CINEMA:
1183 		return "Cinema";
1184 	case HDMI_CONTENT_TYPE_GAME:
1185 		return "Game";
1186 	}
1187 	return "Invalid";
1188 }
1189 
1190 static void hdmi_avi_infoframe_log(const char *level,
1191 				   struct device *dev,
1192 				   const struct hdmi_avi_infoframe *frame)
1193 {
1194 	hdmi_infoframe_log_header(level, dev,
1195 				  (const struct hdmi_any_infoframe *)frame);
1196 
1197 	hdmi_log("    colorspace: %s\n",
1198 			hdmi_colorspace_get_name(frame->colorspace));
1199 	hdmi_log("    scan mode: %s\n",
1200 			hdmi_scan_mode_get_name(frame->scan_mode));
1201 	hdmi_log("    colorimetry: %s\n",
1202 			hdmi_colorimetry_get_name(frame->colorimetry));
1203 	hdmi_log("    picture aspect: %s\n",
1204 			hdmi_picture_aspect_get_name(frame->picture_aspect));
1205 	hdmi_log("    active aspect: %s\n",
1206 			hdmi_active_aspect_get_name(frame->active_aspect));
1207 	hdmi_log("    itc: %s\n", frame->itc ? "IT Content" : "No Data");
1208 	hdmi_log("    extended colorimetry: %s\n",
1209 			hdmi_extended_colorimetry_get_name(frame->extended_colorimetry));
1210 	hdmi_log("    quantization range: %s\n",
1211 			hdmi_quantization_range_get_name(frame->quantization_range));
1212 	hdmi_log("    nups: %s\n", hdmi_nups_get_name(frame->nups));
1213 	hdmi_log("    video code: %u\n", frame->video_code);
1214 	hdmi_log("    ycc quantization range: %s\n",
1215 			hdmi_ycc_quantization_range_get_name(frame->ycc_quantization_range));
1216 	hdmi_log("    hdmi content type: %s\n",
1217 			hdmi_content_type_get_name(frame->content_type));
1218 	hdmi_log("    pixel repeat: %u\n", frame->pixel_repeat);
1219 	hdmi_log("    bar top %u, bottom %u, left %u, right %u\n",
1220 			frame->top_bar, frame->bottom_bar,
1221 			frame->left_bar, frame->right_bar);
1222 }
1223 
1224 static const char *hdmi_spd_sdi_get_name(enum hdmi_spd_sdi sdi)
1225 {
1226 	if (sdi < 0 || sdi > 0xff)
1227 		return "Invalid";
1228 	switch (sdi) {
1229 	case HDMI_SPD_SDI_UNKNOWN:
1230 		return "Unknown";
1231 	case HDMI_SPD_SDI_DSTB:
1232 		return "Digital STB";
1233 	case HDMI_SPD_SDI_DVDP:
1234 		return "DVD Player";
1235 	case HDMI_SPD_SDI_DVHS:
1236 		return "D-VHS";
1237 	case HDMI_SPD_SDI_HDDVR:
1238 		return "HDD Videorecorder";
1239 	case HDMI_SPD_SDI_DVC:
1240 		return "DVC";
1241 	case HDMI_SPD_SDI_DSC:
1242 		return "DSC";
1243 	case HDMI_SPD_SDI_VCD:
1244 		return "Video CD";
1245 	case HDMI_SPD_SDI_GAME:
1246 		return "Game";
1247 	case HDMI_SPD_SDI_PC:
1248 		return "PC General";
1249 	case HDMI_SPD_SDI_BD:
1250 		return "Blu-Ray Disc (BD)";
1251 	case HDMI_SPD_SDI_SACD:
1252 		return "Super Audio CD";
1253 	case HDMI_SPD_SDI_HDDVD:
1254 		return "HD DVD";
1255 	case HDMI_SPD_SDI_PMP:
1256 		return "PMP";
1257 	}
1258 	return "Reserved";
1259 }
1260 
1261 static void hdmi_spd_infoframe_log(const char *level,
1262 				   struct device *dev,
1263 				   const struct hdmi_spd_infoframe *frame)
1264 {
1265 	u8 buf[17];
1266 
1267 	hdmi_infoframe_log_header(level, dev,
1268 				  (const struct hdmi_any_infoframe *)frame);
1269 
1270 	memset(buf, 0, sizeof(buf));
1271 
1272 	strncpy(buf, frame->vendor, 8);
1273 	hdmi_log("    vendor: %s\n", buf);
1274 	strncpy(buf, frame->product, 16);
1275 	hdmi_log("    product: %s\n", buf);
1276 	hdmi_log("    source device information: %s (0x%x)\n",
1277 		hdmi_spd_sdi_get_name(frame->sdi), frame->sdi);
1278 }
1279 
1280 static const char *
1281 hdmi_audio_coding_type_get_name(enum hdmi_audio_coding_type coding_type)
1282 {
1283 	switch (coding_type) {
1284 	case HDMI_AUDIO_CODING_TYPE_STREAM:
1285 		return "Refer to Stream Header";
1286 	case HDMI_AUDIO_CODING_TYPE_PCM:
1287 		return "PCM";
1288 	case HDMI_AUDIO_CODING_TYPE_AC3:
1289 		return "AC-3";
1290 	case HDMI_AUDIO_CODING_TYPE_MPEG1:
1291 		return "MPEG1";
1292 	case HDMI_AUDIO_CODING_TYPE_MP3:
1293 		return "MP3";
1294 	case HDMI_AUDIO_CODING_TYPE_MPEG2:
1295 		return "MPEG2";
1296 	case HDMI_AUDIO_CODING_TYPE_AAC_LC:
1297 		return "AAC";
1298 	case HDMI_AUDIO_CODING_TYPE_DTS:
1299 		return "DTS";
1300 	case HDMI_AUDIO_CODING_TYPE_ATRAC:
1301 		return "ATRAC";
1302 	case HDMI_AUDIO_CODING_TYPE_DSD:
1303 		return "One Bit Audio";
1304 	case HDMI_AUDIO_CODING_TYPE_EAC3:
1305 		return "Dolby Digital +";
1306 	case HDMI_AUDIO_CODING_TYPE_DTS_HD:
1307 		return "DTS-HD";
1308 	case HDMI_AUDIO_CODING_TYPE_MLP:
1309 		return "MAT (MLP)";
1310 	case HDMI_AUDIO_CODING_TYPE_DST:
1311 		return "DST";
1312 	case HDMI_AUDIO_CODING_TYPE_WMA_PRO:
1313 		return "WMA PRO";
1314 	case HDMI_AUDIO_CODING_TYPE_CXT:
1315 		return "Refer to CXT";
1316 	}
1317 	return "Invalid";
1318 }
1319 
1320 static const char *
1321 hdmi_audio_sample_size_get_name(enum hdmi_audio_sample_size sample_size)
1322 {
1323 	switch (sample_size) {
1324 	case HDMI_AUDIO_SAMPLE_SIZE_STREAM:
1325 		return "Refer to Stream Header";
1326 	case HDMI_AUDIO_SAMPLE_SIZE_16:
1327 		return "16 bit";
1328 	case HDMI_AUDIO_SAMPLE_SIZE_20:
1329 		return "20 bit";
1330 	case HDMI_AUDIO_SAMPLE_SIZE_24:
1331 		return "24 bit";
1332 	}
1333 	return "Invalid";
1334 }
1335 
1336 static const char *
1337 hdmi_audio_sample_frequency_get_name(enum hdmi_audio_sample_frequency freq)
1338 {
1339 	switch (freq) {
1340 	case HDMI_AUDIO_SAMPLE_FREQUENCY_STREAM:
1341 		return "Refer to Stream Header";
1342 	case HDMI_AUDIO_SAMPLE_FREQUENCY_32000:
1343 		return "32 kHz";
1344 	case HDMI_AUDIO_SAMPLE_FREQUENCY_44100:
1345 		return "44.1 kHz (CD)";
1346 	case HDMI_AUDIO_SAMPLE_FREQUENCY_48000:
1347 		return "48 kHz";
1348 	case HDMI_AUDIO_SAMPLE_FREQUENCY_88200:
1349 		return "88.2 kHz";
1350 	case HDMI_AUDIO_SAMPLE_FREQUENCY_96000:
1351 		return "96 kHz";
1352 	case HDMI_AUDIO_SAMPLE_FREQUENCY_176400:
1353 		return "176.4 kHz";
1354 	case HDMI_AUDIO_SAMPLE_FREQUENCY_192000:
1355 		return "192 kHz";
1356 	}
1357 	return "Invalid";
1358 }
1359 
1360 static const char *
1361 hdmi_audio_coding_type_ext_get_name(enum hdmi_audio_coding_type_ext ctx)
1362 {
1363 	if (ctx < 0 || ctx > 0x1f)
1364 		return "Invalid";
1365 
1366 	switch (ctx) {
1367 	case HDMI_AUDIO_CODING_TYPE_EXT_CT:
1368 		return "Refer to CT";
1369 	case HDMI_AUDIO_CODING_TYPE_EXT_HE_AAC:
1370 		return "HE AAC";
1371 	case HDMI_AUDIO_CODING_TYPE_EXT_HE_AAC_V2:
1372 		return "HE AAC v2";
1373 	case HDMI_AUDIO_CODING_TYPE_EXT_MPEG_SURROUND:
1374 		return "MPEG SURROUND";
1375 	case HDMI_AUDIO_CODING_TYPE_EXT_MPEG4_HE_AAC:
1376 		return "MPEG-4 HE AAC";
1377 	case HDMI_AUDIO_CODING_TYPE_EXT_MPEG4_HE_AAC_V2:
1378 		return "MPEG-4 HE AAC v2";
1379 	case HDMI_AUDIO_CODING_TYPE_EXT_MPEG4_AAC_LC:
1380 		return "MPEG-4 AAC LC";
1381 	case HDMI_AUDIO_CODING_TYPE_EXT_DRA:
1382 		return "DRA";
1383 	case HDMI_AUDIO_CODING_TYPE_EXT_MPEG4_HE_AAC_SURROUND:
1384 		return "MPEG-4 HE AAC + MPEG Surround";
1385 	case HDMI_AUDIO_CODING_TYPE_EXT_MPEG4_AAC_LC_SURROUND:
1386 		return "MPEG-4 AAC LC + MPEG Surround";
1387 	}
1388 	return "Reserved";
1389 }
1390 
1391 static void hdmi_audio_infoframe_log(const char *level,
1392 				     struct device *dev,
1393 				     const struct hdmi_audio_infoframe *frame)
1394 {
1395 	hdmi_infoframe_log_header(level, dev,
1396 				  (const struct hdmi_any_infoframe *)frame);
1397 
1398 	if (frame->channels)
1399 		hdmi_log("    channels: %u\n", frame->channels - 1);
1400 	else
1401 		hdmi_log("    channels: Refer to stream header\n");
1402 	hdmi_log("    coding type: %s\n",
1403 			hdmi_audio_coding_type_get_name(frame->coding_type));
1404 	hdmi_log("    sample size: %s\n",
1405 			hdmi_audio_sample_size_get_name(frame->sample_size));
1406 	hdmi_log("    sample frequency: %s\n",
1407 			hdmi_audio_sample_frequency_get_name(frame->sample_frequency));
1408 	hdmi_log("    coding type ext: %s\n",
1409 			hdmi_audio_coding_type_ext_get_name(frame->coding_type_ext));
1410 	hdmi_log("    channel allocation: 0x%x\n",
1411 			frame->channel_allocation);
1412 	hdmi_log("    level shift value: %u dB\n",
1413 			frame->level_shift_value);
1414 	hdmi_log("    downmix inhibit: %s\n",
1415 			frame->downmix_inhibit ? "Yes" : "No");
1416 }
1417 
1418 static void hdmi_drm_infoframe_log(const char *level,
1419 				   struct device *dev,
1420 				   const struct hdmi_drm_infoframe *frame)
1421 {
1422 	int i;
1423 
1424 	hdmi_infoframe_log_header(level, dev,
1425 				  (struct hdmi_any_infoframe *)frame);
1426 	hdmi_log("length: %d\n", frame->length);
1427 	hdmi_log("metadata type: %d\n", frame->metadata_type);
1428 	hdmi_log("eotf: %d\n", frame->eotf);
1429 	for (i = 0; i < 3; i++) {
1430 		hdmi_log("x[%d]: %d\n", i, frame->display_primaries[i].x);
1431 		hdmi_log("y[%d]: %d\n", i, frame->display_primaries[i].y);
1432 	}
1433 
1434 	hdmi_log("white point x: %d\n", frame->white_point.x);
1435 	hdmi_log("white point y: %d\n", frame->white_point.y);
1436 
1437 	hdmi_log("max_display_mastering_luminance: %d\n",
1438 		 frame->max_display_mastering_luminance);
1439 	hdmi_log("min_display_mastering_luminance: %d\n",
1440 		 frame->min_display_mastering_luminance);
1441 
1442 	hdmi_log("max_cll: %d\n", frame->max_cll);
1443 	hdmi_log("max_fall: %d\n", frame->max_fall);
1444 }
1445 
1446 static const char *
1447 hdmi_3d_structure_get_name(enum hdmi_3d_structure s3d_struct)
1448 {
1449 	if (s3d_struct < 0 || s3d_struct > 0xf)
1450 		return "Invalid";
1451 
1452 	switch (s3d_struct) {
1453 	case HDMI_3D_STRUCTURE_FRAME_PACKING:
1454 		return "Frame Packing";
1455 	case HDMI_3D_STRUCTURE_FIELD_ALTERNATIVE:
1456 		return "Field Alternative";
1457 	case HDMI_3D_STRUCTURE_LINE_ALTERNATIVE:
1458 		return "Line Alternative";
1459 	case HDMI_3D_STRUCTURE_SIDE_BY_SIDE_FULL:
1460 		return "Side-by-side (Full)";
1461 	case HDMI_3D_STRUCTURE_L_DEPTH:
1462 		return "L + Depth";
1463 	case HDMI_3D_STRUCTURE_L_DEPTH_GFX_GFX_DEPTH:
1464 		return "L + Depth + Graphics + Graphics-depth";
1465 	case HDMI_3D_STRUCTURE_TOP_AND_BOTTOM:
1466 		return "Top-and-Bottom";
1467 	case HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF:
1468 		return "Side-by-side (Half)";
1469 	default:
1470 		break;
1471 	}
1472 	return "Reserved";
1473 }
1474 
1475 static void
1476 hdmi_vendor_any_infoframe_log(const char *level,
1477 			      struct device *dev,
1478 			      const union hdmi_vendor_any_infoframe *frame)
1479 {
1480 	const struct hdmi_vendor_infoframe *hvf = &frame->hdmi;
1481 
1482 	hdmi_infoframe_log_header(level, dev,
1483 				  (const struct hdmi_any_infoframe *)frame);
1484 
1485 	if (frame->any.oui != HDMI_IEEE_OUI) {
1486 		hdmi_log("    not a HDMI vendor infoframe\n");
1487 		return;
1488 	}
1489 	if (hvf->vic == 0 && hvf->s3d_struct == HDMI_3D_STRUCTURE_INVALID) {
1490 		hdmi_log("    empty frame\n");
1491 		return;
1492 	}
1493 
1494 	if (hvf->vic)
1495 		hdmi_log("    HDMI VIC: %u\n", hvf->vic);
1496 	if (hvf->s3d_struct != HDMI_3D_STRUCTURE_INVALID) {
1497 		hdmi_log("    3D structure: %s\n",
1498 				hdmi_3d_structure_get_name(hvf->s3d_struct));
1499 		if (hvf->s3d_struct >= HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF)
1500 			hdmi_log("    3D extension data: %d\n",
1501 					hvf->s3d_ext_data);
1502 	}
1503 }
1504 
1505 /**
1506  * hdmi_infoframe_log() - log info of HDMI infoframe
1507  * @level: logging level
1508  * @dev: device
1509  * @frame: HDMI infoframe
1510  */
1511 void hdmi_infoframe_log(const char *level,
1512 			struct device *dev,
1513 			const union hdmi_infoframe *frame)
1514 {
1515 	switch (frame->any.type) {
1516 	case HDMI_INFOFRAME_TYPE_AVI:
1517 		hdmi_avi_infoframe_log(level, dev, &frame->avi);
1518 		break;
1519 	case HDMI_INFOFRAME_TYPE_SPD:
1520 		hdmi_spd_infoframe_log(level, dev, &frame->spd);
1521 		break;
1522 	case HDMI_INFOFRAME_TYPE_AUDIO:
1523 		hdmi_audio_infoframe_log(level, dev, &frame->audio);
1524 		break;
1525 	case HDMI_INFOFRAME_TYPE_VENDOR:
1526 		hdmi_vendor_any_infoframe_log(level, dev, &frame->vendor);
1527 		break;
1528 	case HDMI_INFOFRAME_TYPE_DRM:
1529 		hdmi_drm_infoframe_log(level, dev, &frame->drm);
1530 		break;
1531 	}
1532 }
1533 EXPORT_SYMBOL(hdmi_infoframe_log);
1534 
1535 /**
1536  * hdmi_avi_infoframe_unpack() - unpack binary buffer to a HDMI AVI infoframe
1537  * @frame: HDMI AVI infoframe
1538  * @buffer: source buffer
1539  * @size: size of buffer
1540  *
1541  * Unpacks the information contained in binary @buffer into a structured
1542  * @frame of the HDMI Auxiliary Video (AVI) information frame.
1543  * Also verifies the checksum as required by section 5.3.5 of the HDMI 1.4
1544  * specification.
1545  *
1546  * Returns 0 on success or a negative error code on failure.
1547  */
1548 static int hdmi_avi_infoframe_unpack(struct hdmi_avi_infoframe *frame,
1549 				     const void *buffer, size_t size)
1550 {
1551 	const u8 *ptr = buffer;
1552 
1553 	if (size < HDMI_INFOFRAME_SIZE(AVI))
1554 		return -EINVAL;
1555 
1556 	if (ptr[0] != HDMI_INFOFRAME_TYPE_AVI ||
1557 	    ptr[1] != 2 ||
1558 	    ptr[2] != HDMI_AVI_INFOFRAME_SIZE)
1559 		return -EINVAL;
1560 
1561 	if (hdmi_infoframe_checksum(buffer, HDMI_INFOFRAME_SIZE(AVI)) != 0)
1562 		return -EINVAL;
1563 
1564 	hdmi_avi_infoframe_init(frame);
1565 
1566 	ptr += HDMI_INFOFRAME_HEADER_SIZE;
1567 
1568 	frame->colorspace = (ptr[0] >> 5) & 0x3;
1569 	if (ptr[0] & 0x10)
1570 		frame->active_aspect = ptr[1] & 0xf;
1571 	if (ptr[0] & 0x8) {
1572 		frame->top_bar = (ptr[6] << 8) | ptr[5];
1573 		frame->bottom_bar = (ptr[8] << 8) | ptr[7];
1574 	}
1575 	if (ptr[0] & 0x4) {
1576 		frame->left_bar = (ptr[10] << 8) | ptr[9];
1577 		frame->right_bar = (ptr[12] << 8) | ptr[11];
1578 	}
1579 	frame->scan_mode = ptr[0] & 0x3;
1580 
1581 	frame->colorimetry = (ptr[1] >> 6) & 0x3;
1582 	frame->picture_aspect = (ptr[1] >> 4) & 0x3;
1583 	frame->active_aspect = ptr[1] & 0xf;
1584 
1585 	frame->itc = ptr[2] & 0x80 ? true : false;
1586 	frame->extended_colorimetry = (ptr[2] >> 4) & 0x7;
1587 	frame->quantization_range = (ptr[2] >> 2) & 0x3;
1588 	frame->nups = ptr[2] & 0x3;
1589 
1590 	frame->video_code = ptr[3] & 0x7f;
1591 	frame->ycc_quantization_range = (ptr[4] >> 6) & 0x3;
1592 	frame->content_type = (ptr[4] >> 4) & 0x3;
1593 
1594 	frame->pixel_repeat = ptr[4] & 0xf;
1595 
1596 	return 0;
1597 }
1598 
1599 /**
1600  * hdmi_spd_infoframe_unpack() - unpack binary buffer to a HDMI SPD infoframe
1601  * @frame: HDMI SPD infoframe
1602  * @buffer: source buffer
1603  * @size: size of buffer
1604  *
1605  * Unpacks the information contained in binary @buffer into a structured
1606  * @frame of the HDMI Source Product Description (SPD) information frame.
1607  * Also verifies the checksum as required by section 5.3.5 of the HDMI 1.4
1608  * specification.
1609  *
1610  * Returns 0 on success or a negative error code on failure.
1611  */
1612 static int hdmi_spd_infoframe_unpack(struct hdmi_spd_infoframe *frame,
1613 				     const void *buffer, size_t size)
1614 {
1615 	const u8 *ptr = buffer;
1616 	int ret;
1617 
1618 	if (size < HDMI_INFOFRAME_SIZE(SPD))
1619 		return -EINVAL;
1620 
1621 	if (ptr[0] != HDMI_INFOFRAME_TYPE_SPD ||
1622 	    ptr[1] != 1 ||
1623 	    ptr[2] != HDMI_SPD_INFOFRAME_SIZE) {
1624 		return -EINVAL;
1625 	}
1626 
1627 	if (hdmi_infoframe_checksum(buffer, HDMI_INFOFRAME_SIZE(SPD)) != 0)
1628 		return -EINVAL;
1629 
1630 	ptr += HDMI_INFOFRAME_HEADER_SIZE;
1631 
1632 	ret = hdmi_spd_infoframe_init(frame, ptr, ptr + 8);
1633 	if (ret)
1634 		return ret;
1635 
1636 	frame->sdi = ptr[24];
1637 
1638 	return 0;
1639 }
1640 
1641 /**
1642  * hdmi_audio_infoframe_unpack() - unpack binary buffer to a HDMI AUDIO infoframe
1643  * @frame: HDMI Audio infoframe
1644  * @buffer: source buffer
1645  * @size: size of buffer
1646  *
1647  * Unpacks the information contained in binary @buffer into a structured
1648  * @frame of the HDMI Audio information frame.
1649  * Also verifies the checksum as required by section 5.3.5 of the HDMI 1.4
1650  * specification.
1651  *
1652  * Returns 0 on success or a negative error code on failure.
1653  */
1654 static int hdmi_audio_infoframe_unpack(struct hdmi_audio_infoframe *frame,
1655 				       const void *buffer, size_t size)
1656 {
1657 	const u8 *ptr = buffer;
1658 	int ret;
1659 
1660 	if (size < HDMI_INFOFRAME_SIZE(AUDIO))
1661 		return -EINVAL;
1662 
1663 	if (ptr[0] != HDMI_INFOFRAME_TYPE_AUDIO ||
1664 	    ptr[1] != 1 ||
1665 	    ptr[2] != HDMI_AUDIO_INFOFRAME_SIZE) {
1666 		return -EINVAL;
1667 	}
1668 
1669 	if (hdmi_infoframe_checksum(buffer, HDMI_INFOFRAME_SIZE(AUDIO)) != 0)
1670 		return -EINVAL;
1671 
1672 	ret = hdmi_audio_infoframe_init(frame);
1673 	if (ret)
1674 		return ret;
1675 
1676 	ptr += HDMI_INFOFRAME_HEADER_SIZE;
1677 
1678 	frame->channels = ptr[0] & 0x7;
1679 	frame->coding_type = (ptr[0] >> 4) & 0xf;
1680 	frame->sample_size = ptr[1] & 0x3;
1681 	frame->sample_frequency = (ptr[1] >> 2) & 0x7;
1682 	frame->coding_type_ext = ptr[2] & 0x1f;
1683 	frame->channel_allocation = ptr[3];
1684 	frame->level_shift_value = (ptr[4] >> 3) & 0xf;
1685 	frame->downmix_inhibit = ptr[4] & 0x80 ? true : false;
1686 
1687 	return 0;
1688 }
1689 
1690 /**
1691  * hdmi_vendor_infoframe_unpack() - unpack binary buffer to a HDMI vendor infoframe
1692  * @frame: HDMI Vendor infoframe
1693  * @buffer: source buffer
1694  * @size: size of buffer
1695  *
1696  * Unpacks the information contained in binary @buffer into a structured
1697  * @frame of the HDMI Vendor information frame.
1698  * Also verifies the checksum as required by section 5.3.5 of the HDMI 1.4
1699  * specification.
1700  *
1701  * Returns 0 on success or a negative error code on failure.
1702  */
1703 static int
1704 hdmi_vendor_any_infoframe_unpack(union hdmi_vendor_any_infoframe *frame,
1705 				 const void *buffer, size_t size)
1706 {
1707 	const u8 *ptr = buffer;
1708 	size_t length;
1709 	int ret;
1710 	u8 hdmi_video_format;
1711 	struct hdmi_vendor_infoframe *hvf = &frame->hdmi;
1712 
1713 	if (size < HDMI_INFOFRAME_HEADER_SIZE)
1714 		return -EINVAL;
1715 
1716 	if (ptr[0] != HDMI_INFOFRAME_TYPE_VENDOR ||
1717 	    ptr[1] != 1 ||
1718 	    (ptr[2] != 4 && ptr[2] != 5 && ptr[2] != 6))
1719 		return -EINVAL;
1720 
1721 	length = ptr[2];
1722 
1723 	if (size < HDMI_INFOFRAME_HEADER_SIZE + length)
1724 		return -EINVAL;
1725 
1726 	if (hdmi_infoframe_checksum(buffer,
1727 				    HDMI_INFOFRAME_HEADER_SIZE + length) != 0)
1728 		return -EINVAL;
1729 
1730 	ptr += HDMI_INFOFRAME_HEADER_SIZE;
1731 
1732 	/* HDMI OUI */
1733 	if ((ptr[0] != 0x03) ||
1734 	    (ptr[1] != 0x0c) ||
1735 	    (ptr[2] != 0x00))
1736 		return -EINVAL;
1737 
1738 	hdmi_video_format = ptr[3] >> 5;
1739 
1740 	if (hdmi_video_format > 0x2)
1741 		return -EINVAL;
1742 
1743 	ret = hdmi_vendor_infoframe_init(hvf);
1744 	if (ret)
1745 		return ret;
1746 
1747 	hvf->length = length;
1748 
1749 	if (hdmi_video_format == 0x2) {
1750 		if (length != 5 && length != 6)
1751 			return -EINVAL;
1752 		hvf->s3d_struct = ptr[4] >> 4;
1753 		if (hvf->s3d_struct >= HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF) {
1754 			if (length != 6)
1755 				return -EINVAL;
1756 			hvf->s3d_ext_data = ptr[5] >> 4;
1757 		}
1758 	} else if (hdmi_video_format == 0x1) {
1759 		if (length != 5)
1760 			return -EINVAL;
1761 		hvf->vic = ptr[4];
1762 	} else {
1763 		if (length != 4)
1764 			return -EINVAL;
1765 	}
1766 
1767 	return 0;
1768 }
1769 
1770 /**
1771  * hdmi_drm_infoframe_unpack_only() - unpack binary buffer of CTA-861-G DRM
1772  *                                    infoframe DataBytes to a HDMI DRM
1773  *                                    infoframe
1774  * @frame: HDMI DRM infoframe
1775  * @buffer: source buffer
1776  * @size: size of buffer
1777  *
1778  * Unpacks CTA-861-G DRM infoframe DataBytes contained in the binary @buffer
1779  * into a structured @frame of the HDMI Dynamic Range and Mastering (DRM)
1780  * infoframe.
1781  *
1782  * Returns 0 on success or a negative error code on failure.
1783  */
1784 int hdmi_drm_infoframe_unpack_only(struct hdmi_drm_infoframe *frame,
1785 				   const void *buffer, size_t size)
1786 {
1787 	const u8 *ptr = buffer;
1788 	const u8 *temp;
1789 	u8 x_lsb, x_msb;
1790 	u8 y_lsb, y_msb;
1791 	int ret;
1792 	int i;
1793 
1794 	if (size < HDMI_DRM_INFOFRAME_SIZE)
1795 		return -EINVAL;
1796 
1797 	ret = hdmi_drm_infoframe_init(frame);
1798 	if (ret)
1799 		return ret;
1800 
1801 	frame->eotf = ptr[0] & 0x7;
1802 	frame->metadata_type = ptr[1] & 0x7;
1803 
1804 	temp = ptr + 2;
1805 	for (i = 0; i < 3; i++) {
1806 		x_lsb = *temp++;
1807 		x_msb = *temp++;
1808 		frame->display_primaries[i].x = (x_msb << 8) | x_lsb;
1809 		y_lsb = *temp++;
1810 		y_msb = *temp++;
1811 		frame->display_primaries[i].y = (y_msb << 8) | y_lsb;
1812 	}
1813 
1814 	frame->white_point.x = (ptr[15] << 8) | ptr[14];
1815 	frame->white_point.y = (ptr[17] << 8) | ptr[16];
1816 
1817 	frame->max_display_mastering_luminance = (ptr[19] << 8) | ptr[18];
1818 	frame->min_display_mastering_luminance = (ptr[21] << 8) | ptr[20];
1819 	frame->max_cll = (ptr[23] << 8) | ptr[22];
1820 	frame->max_fall = (ptr[25] << 8) | ptr[24];
1821 
1822 	return 0;
1823 }
1824 EXPORT_SYMBOL(hdmi_drm_infoframe_unpack_only);
1825 
1826 /**
1827  * hdmi_drm_infoframe_unpack() - unpack binary buffer to a HDMI DRM infoframe
1828  * @frame: HDMI DRM infoframe
1829  * @buffer: source buffer
1830  * @size: size of buffer
1831  *
1832  * Unpacks the CTA-861-G DRM infoframe contained in the binary @buffer into
1833  * a structured @frame of the HDMI Dynamic Range and Mastering (DRM)
1834  * infoframe. It also verifies the checksum as required by section 5.3.5 of
1835  * the HDMI 1.4 specification.
1836  *
1837  * Returns 0 on success or a negative error code on failure.
1838  */
1839 static int hdmi_drm_infoframe_unpack(struct hdmi_drm_infoframe *frame,
1840 				     const void *buffer, size_t size)
1841 {
1842 	const u8 *ptr = buffer;
1843 	int ret;
1844 
1845 	if (size < HDMI_INFOFRAME_SIZE(DRM))
1846 		return -EINVAL;
1847 
1848 	if (ptr[0] != HDMI_INFOFRAME_TYPE_DRM ||
1849 	    ptr[1] != 1 ||
1850 	    ptr[2] != HDMI_DRM_INFOFRAME_SIZE)
1851 		return -EINVAL;
1852 
1853 	if (hdmi_infoframe_checksum(buffer, HDMI_INFOFRAME_SIZE(DRM)) != 0)
1854 		return -EINVAL;
1855 
1856 	ret = hdmi_drm_infoframe_unpack_only(frame, ptr + HDMI_INFOFRAME_HEADER_SIZE,
1857 					     size - HDMI_INFOFRAME_HEADER_SIZE);
1858 	return ret;
1859 }
1860 
1861 /**
1862  * hdmi_infoframe_unpack() - unpack binary buffer to a HDMI infoframe
1863  * @frame: HDMI infoframe
1864  * @buffer: source buffer
1865  * @size: size of buffer
1866  *
1867  * Unpacks the information contained in binary buffer @buffer into a structured
1868  * @frame of a HDMI infoframe.
1869  * Also verifies the checksum as required by section 5.3.5 of the HDMI 1.4
1870  * specification.
1871  *
1872  * Returns 0 on success or a negative error code on failure.
1873  */
1874 int hdmi_infoframe_unpack(union hdmi_infoframe *frame,
1875 			  const void *buffer, size_t size)
1876 {
1877 	int ret;
1878 	const u8 *ptr = buffer;
1879 
1880 	if (size < HDMI_INFOFRAME_HEADER_SIZE)
1881 		return -EINVAL;
1882 
1883 	switch (ptr[0]) {
1884 	case HDMI_INFOFRAME_TYPE_AVI:
1885 		ret = hdmi_avi_infoframe_unpack(&frame->avi, buffer, size);
1886 		break;
1887 	case HDMI_INFOFRAME_TYPE_DRM:
1888 		ret = hdmi_drm_infoframe_unpack(&frame->drm, buffer, size);
1889 		break;
1890 	case HDMI_INFOFRAME_TYPE_SPD:
1891 		ret = hdmi_spd_infoframe_unpack(&frame->spd, buffer, size);
1892 		break;
1893 	case HDMI_INFOFRAME_TYPE_AUDIO:
1894 		ret = hdmi_audio_infoframe_unpack(&frame->audio, buffer, size);
1895 		break;
1896 	case HDMI_INFOFRAME_TYPE_VENDOR:
1897 		ret = hdmi_vendor_any_infoframe_unpack(&frame->vendor, buffer, size);
1898 		break;
1899 	default:
1900 		ret = -EINVAL;
1901 		break;
1902 	}
1903 
1904 	return ret;
1905 }
1906 EXPORT_SYMBOL(hdmi_infoframe_unpack);
1907