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