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