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