Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (C) 2012 Avionic Design GmbH
0003  *
0004  * Permission is hereby granted, free of charge, to any person obtaining a
0005  * copy of this software and associated documentation files (the "Software"),
0006  * to deal in the Software without restriction, including without limitation
0007  * the rights to use, copy, modify, merge, publish, distribute, sub license,
0008  * and/or sell copies of the Software, and to permit persons to whom the
0009  * Software is furnished to do so, subject to the following conditions:
0010  *
0011  * The above copyright notice and this permission notice (including the
0012  * next paragraph) shall be included in all copies or substantial portions
0013  * of the Software.
0014  *
0015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0016  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0017  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
0018  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0019  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
0020  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
0021  * DEALINGS IN THE SOFTWARE.
0022  */
0023 
0024 #include <linux/bitops.h>
0025 #include <linux/bug.h>
0026 #include <linux/errno.h>
0027 #include <linux/export.h>
0028 #include <linux/hdmi.h>
0029 #include <linux/string.h>
0030 #include <linux/device.h>
0031 
0032 #define hdmi_log(fmt, ...) dev_printk(level, dev, fmt, ##__VA_ARGS__)
0033 
0034 static u8 hdmi_infoframe_checksum(const u8 *ptr, size_t size)
0035 {
0036     u8 csum = 0;
0037     size_t i;
0038 
0039     /* compute checksum */
0040     for (i = 0; i < size; i++)
0041         csum += ptr[i];
0042 
0043     return 256 - csum;
0044 }
0045 
0046 static void hdmi_infoframe_set_checksum(void *buffer, size_t size)
0047 {
0048     u8 *ptr = buffer;
0049 
0050     ptr[3] = hdmi_infoframe_checksum(buffer, size);
0051 }
0052 
0053 /**
0054  * hdmi_avi_infoframe_init() - initialize an HDMI AVI infoframe
0055  * @frame: HDMI AVI infoframe
0056  */
0057 void hdmi_avi_infoframe_init(struct hdmi_avi_infoframe *frame)
0058 {
0059     memset(frame, 0, sizeof(*frame));
0060 
0061     frame->type = HDMI_INFOFRAME_TYPE_AVI;
0062     frame->version = 2;
0063     frame->length = HDMI_AVI_INFOFRAME_SIZE;
0064 }
0065 EXPORT_SYMBOL(hdmi_avi_infoframe_init);
0066 
0067 static int hdmi_avi_infoframe_check_only(const struct hdmi_avi_infoframe *frame)
0068 {
0069     if (frame->type != HDMI_INFOFRAME_TYPE_AVI ||
0070         frame->version != 2 ||
0071         frame->length != HDMI_AVI_INFOFRAME_SIZE)
0072         return -EINVAL;
0073 
0074     if (frame->picture_aspect > HDMI_PICTURE_ASPECT_16_9)
0075         return -EINVAL;
0076 
0077     return 0;
0078 }
0079 
0080 /**
0081  * hdmi_avi_infoframe_check() - check a HDMI AVI infoframe
0082  * @frame: HDMI AVI infoframe
0083  *
0084  * Validates that the infoframe is consistent and updates derived fields
0085  * (eg. length) based on other fields.
0086  *
0087  * Returns 0 on success or a negative error code on failure.
0088  */
0089 int hdmi_avi_infoframe_check(struct hdmi_avi_infoframe *frame)
0090 {
0091     return hdmi_avi_infoframe_check_only(frame);
0092 }
0093 EXPORT_SYMBOL(hdmi_avi_infoframe_check);
0094 
0095 /**
0096  * hdmi_avi_infoframe_pack_only() - write HDMI AVI infoframe to binary buffer
0097  * @frame: HDMI AVI infoframe
0098  * @buffer: destination buffer
0099  * @size: size of buffer
0100  *
0101  * Packs the information contained in the @frame structure into a binary
0102  * representation that can be written into the corresponding controller
0103  * registers. Also computes the checksum as required by section 5.3.5 of
0104  * the HDMI 1.4 specification.
0105  *
0106  * Returns the number of bytes packed into the binary buffer or a negative
0107  * error code on failure.
0108  */
0109 ssize_t hdmi_avi_infoframe_pack_only(const struct hdmi_avi_infoframe *frame,
0110                      void *buffer, size_t size)
0111 {
0112     u8 *ptr = buffer;
0113     size_t length;
0114     int ret;
0115 
0116     ret = hdmi_avi_infoframe_check_only(frame);
0117     if (ret)
0118         return ret;
0119 
0120     length = HDMI_INFOFRAME_HEADER_SIZE + frame->length;
0121 
0122     if (size < length)
0123         return -ENOSPC;
0124 
0125     memset(buffer, 0, size);
0126 
0127     ptr[0] = frame->type;
0128     ptr[1] = frame->version;
0129     ptr[2] = frame->length;
0130     ptr[3] = 0; /* checksum */
0131 
0132     /* start infoframe payload */
0133     ptr += HDMI_INFOFRAME_HEADER_SIZE;
0134 
0135     ptr[0] = ((frame->colorspace & 0x3) << 5) | (frame->scan_mode & 0x3);
0136 
0137     /*
0138      * Data byte 1, bit 4 has to be set if we provide the active format
0139      * aspect ratio
0140      */
0141     if (frame->active_aspect & 0xf)
0142         ptr[0] |= BIT(4);
0143 
0144     /* Bit 3 and 2 indicate if we transmit horizontal/vertical bar data */
0145     if (frame->top_bar || frame->bottom_bar)
0146         ptr[0] |= BIT(3);
0147 
0148     if (frame->left_bar || frame->right_bar)
0149         ptr[0] |= BIT(2);
0150 
0151     ptr[1] = ((frame->colorimetry & 0x3) << 6) |
0152          ((frame->picture_aspect & 0x3) << 4) |
0153          (frame->active_aspect & 0xf);
0154 
0155     ptr[2] = ((frame->extended_colorimetry & 0x7) << 4) |
0156          ((frame->quantization_range & 0x3) << 2) |
0157          (frame->nups & 0x3);
0158 
0159     if (frame->itc)
0160         ptr[2] |= BIT(7);
0161 
0162     ptr[3] = frame->video_code & 0x7f;
0163 
0164     ptr[4] = ((frame->ycc_quantization_range & 0x3) << 6) |
0165          ((frame->content_type & 0x3) << 4) |
0166          (frame->pixel_repeat & 0xf);
0167 
0168     ptr[5] = frame->top_bar & 0xff;
0169     ptr[6] = (frame->top_bar >> 8) & 0xff;
0170     ptr[7] = frame->bottom_bar & 0xff;
0171     ptr[8] = (frame->bottom_bar >> 8) & 0xff;
0172     ptr[9] = frame->left_bar & 0xff;
0173     ptr[10] = (frame->left_bar >> 8) & 0xff;
0174     ptr[11] = frame->right_bar & 0xff;
0175     ptr[12] = (frame->right_bar >> 8) & 0xff;
0176 
0177     hdmi_infoframe_set_checksum(buffer, length);
0178 
0179     return length;
0180 }
0181 EXPORT_SYMBOL(hdmi_avi_infoframe_pack_only);
0182 
0183 /**
0184  * hdmi_avi_infoframe_pack() - check a HDMI AVI infoframe,
0185  *                             and write it to binary buffer
0186  * @frame: HDMI AVI infoframe
0187  * @buffer: destination buffer
0188  * @size: size of buffer
0189  *
0190  * Validates that the infoframe is consistent and updates derived fields
0191  * (eg. length) based on other fields, after which it packs the information
0192  * contained in the @frame structure into a binary representation that
0193  * can be written into the corresponding controller registers. This function
0194  * also computes the checksum as required by section 5.3.5 of the HDMI 1.4
0195  * specification.
0196  *
0197  * Returns the number of bytes packed into the binary buffer or a negative
0198  * error code on failure.
0199  */
0200 ssize_t hdmi_avi_infoframe_pack(struct hdmi_avi_infoframe *frame,
0201                 void *buffer, size_t size)
0202 {
0203     int ret;
0204 
0205     ret = hdmi_avi_infoframe_check(frame);
0206     if (ret)
0207         return ret;
0208 
0209     return hdmi_avi_infoframe_pack_only(frame, buffer, size);
0210 }
0211 EXPORT_SYMBOL(hdmi_avi_infoframe_pack);
0212 
0213 /**
0214  * hdmi_spd_infoframe_init() - initialize an HDMI SPD infoframe
0215  * @frame: HDMI SPD infoframe
0216  * @vendor: vendor string
0217  * @product: product string
0218  *
0219  * Returns 0 on success or a negative error code on failure.
0220  */
0221 int hdmi_spd_infoframe_init(struct hdmi_spd_infoframe *frame,
0222                 const char *vendor, const char *product)
0223 {
0224     size_t len;
0225 
0226     memset(frame, 0, sizeof(*frame));
0227 
0228     frame->type = HDMI_INFOFRAME_TYPE_SPD;
0229     frame->version = 1;
0230     frame->length = HDMI_SPD_INFOFRAME_SIZE;
0231 
0232     len = strlen(vendor);
0233     memcpy(frame->vendor, vendor, min(len, sizeof(frame->vendor)));
0234     len = strlen(product);
0235     memcpy(frame->product, product, min(len, sizeof(frame->product)));
0236 
0237     return 0;
0238 }
0239 EXPORT_SYMBOL(hdmi_spd_infoframe_init);
0240 
0241 static int hdmi_spd_infoframe_check_only(const struct hdmi_spd_infoframe *frame)
0242 {
0243     if (frame->type != HDMI_INFOFRAME_TYPE_SPD ||
0244         frame->version != 1 ||
0245         frame->length != HDMI_SPD_INFOFRAME_SIZE)
0246         return -EINVAL;
0247 
0248     return 0;
0249 }
0250 
0251 /**
0252  * hdmi_spd_infoframe_check() - check a HDMI SPD infoframe
0253  * @frame: HDMI SPD infoframe
0254  *
0255  * Validates that the infoframe is consistent and updates derived fields
0256  * (eg. length) based on other fields.
0257  *
0258  * Returns 0 on success or a negative error code on failure.
0259  */
0260 int hdmi_spd_infoframe_check(struct hdmi_spd_infoframe *frame)
0261 {
0262     return hdmi_spd_infoframe_check_only(frame);
0263 }
0264 EXPORT_SYMBOL(hdmi_spd_infoframe_check);
0265 
0266 /**
0267  * hdmi_spd_infoframe_pack_only() - write HDMI SPD infoframe to binary buffer
0268  * @frame: HDMI SPD infoframe
0269  * @buffer: destination buffer
0270  * @size: size of buffer
0271  *
0272  * Packs the information contained in the @frame structure into a binary
0273  * representation that can be written into the corresponding controller
0274  * registers. Also computes the checksum as required by section 5.3.5 of
0275  * the HDMI 1.4 specification.
0276  *
0277  * Returns the number of bytes packed into the binary buffer or a negative
0278  * error code on failure.
0279  */
0280 ssize_t hdmi_spd_infoframe_pack_only(const struct hdmi_spd_infoframe *frame,
0281                      void *buffer, size_t size)
0282 {
0283     u8 *ptr = buffer;
0284     size_t length;
0285     int ret;
0286 
0287     ret = hdmi_spd_infoframe_check_only(frame);
0288     if (ret)
0289         return ret;
0290 
0291     length = HDMI_INFOFRAME_HEADER_SIZE + frame->length;
0292 
0293     if (size < length)
0294         return -ENOSPC;
0295 
0296     memset(buffer, 0, size);
0297 
0298     ptr[0] = frame->type;
0299     ptr[1] = frame->version;
0300     ptr[2] = frame->length;
0301     ptr[3] = 0; /* checksum */
0302 
0303     /* start infoframe payload */
0304     ptr += HDMI_INFOFRAME_HEADER_SIZE;
0305 
0306     memcpy(ptr, frame->vendor, sizeof(frame->vendor));
0307     memcpy(ptr + 8, frame->product, sizeof(frame->product));
0308 
0309     ptr[24] = frame->sdi;
0310 
0311     hdmi_infoframe_set_checksum(buffer, length);
0312 
0313     return length;
0314 }
0315 EXPORT_SYMBOL(hdmi_spd_infoframe_pack_only);
0316 
0317 /**
0318  * hdmi_spd_infoframe_pack() - check a HDMI SPD infoframe,
0319  *                             and write it to binary buffer
0320  * @frame: HDMI SPD infoframe
0321  * @buffer: destination buffer
0322  * @size: size of buffer
0323  *
0324  * Validates that the infoframe is consistent and updates derived fields
0325  * (eg. length) based on other fields, after which it packs the information
0326  * contained in the @frame structure into a binary representation that
0327  * can be written into the corresponding controller registers. This function
0328  * also computes the checksum as required by section 5.3.5 of the HDMI 1.4
0329  * specification.
0330  *
0331  * Returns the number of bytes packed into the binary buffer or a negative
0332  * error code on failure.
0333  */
0334 ssize_t hdmi_spd_infoframe_pack(struct hdmi_spd_infoframe *frame,
0335                 void *buffer, size_t size)
0336 {
0337     int ret;
0338 
0339     ret = hdmi_spd_infoframe_check(frame);
0340     if (ret)
0341         return ret;
0342 
0343     return hdmi_spd_infoframe_pack_only(frame, buffer, size);
0344 }
0345 EXPORT_SYMBOL(hdmi_spd_infoframe_pack);
0346 
0347 /**
0348  * hdmi_audio_infoframe_init() - initialize an HDMI audio infoframe
0349  * @frame: HDMI audio infoframe
0350  *
0351  * Returns 0 on success or a negative error code on failure.
0352  */
0353 int hdmi_audio_infoframe_init(struct hdmi_audio_infoframe *frame)
0354 {
0355     memset(frame, 0, sizeof(*frame));
0356 
0357     frame->type = HDMI_INFOFRAME_TYPE_AUDIO;
0358     frame->version = 1;
0359     frame->length = HDMI_AUDIO_INFOFRAME_SIZE;
0360 
0361     return 0;
0362 }
0363 EXPORT_SYMBOL(hdmi_audio_infoframe_init);
0364 
0365 static int hdmi_audio_infoframe_check_only(const struct hdmi_audio_infoframe *frame)
0366 {
0367     if (frame->type != HDMI_INFOFRAME_TYPE_AUDIO ||
0368         frame->version != 1 ||
0369         frame->length != HDMI_AUDIO_INFOFRAME_SIZE)
0370         return -EINVAL;
0371 
0372     return 0;
0373 }
0374 
0375 /**
0376  * hdmi_audio_infoframe_check() - check a HDMI audio infoframe
0377  * @frame: HDMI audio infoframe
0378  *
0379  * Validates that the infoframe is consistent and updates derived fields
0380  * (eg. length) based on other fields.
0381  *
0382  * Returns 0 on success or a negative error code on failure.
0383  */
0384 int hdmi_audio_infoframe_check(struct hdmi_audio_infoframe *frame)
0385 {
0386     return hdmi_audio_infoframe_check_only(frame);
0387 }
0388 EXPORT_SYMBOL(hdmi_audio_infoframe_check);
0389 
0390 /**
0391  * hdmi_audio_infoframe_pack_only() - write HDMI audio infoframe to binary buffer
0392  * @frame: HDMI audio infoframe
0393  * @buffer: destination buffer
0394  * @size: size of buffer
0395  *
0396  * Packs the information contained in the @frame structure into a binary
0397  * representation that can be written into the corresponding controller
0398  * registers. Also computes the checksum as required by section 5.3.5 of
0399  * the HDMI 1.4 specification.
0400  *
0401  * Returns the number of bytes packed into the binary buffer or a negative
0402  * error code on failure.
0403  */
0404 ssize_t hdmi_audio_infoframe_pack_only(const struct hdmi_audio_infoframe *frame,
0405                        void *buffer, size_t size)
0406 {
0407     unsigned char channels;
0408     u8 *ptr = buffer;
0409     size_t length;
0410     int ret;
0411 
0412     ret = hdmi_audio_infoframe_check_only(frame);
0413     if (ret)
0414         return ret;
0415 
0416     length = HDMI_INFOFRAME_HEADER_SIZE + frame->length;
0417 
0418     if (size < length)
0419         return -ENOSPC;
0420 
0421     memset(buffer, 0, size);
0422 
0423     if (frame->channels >= 2)
0424         channels = frame->channels - 1;
0425     else
0426         channels = 0;
0427 
0428     ptr[0] = frame->type;
0429     ptr[1] = frame->version;
0430     ptr[2] = frame->length;
0431     ptr[3] = 0; /* checksum */
0432 
0433     /* start infoframe payload */
0434     ptr += HDMI_INFOFRAME_HEADER_SIZE;
0435 
0436     ptr[0] = ((frame->coding_type & 0xf) << 4) | (channels & 0x7);
0437     ptr[1] = ((frame->sample_frequency & 0x7) << 2) |
0438          (frame->sample_size & 0x3);
0439     ptr[2] = frame->coding_type_ext & 0x1f;
0440     ptr[3] = frame->channel_allocation;
0441     ptr[4] = (frame->level_shift_value & 0xf) << 3;
0442 
0443     if (frame->downmix_inhibit)
0444         ptr[4] |= BIT(7);
0445 
0446     hdmi_infoframe_set_checksum(buffer, length);
0447 
0448     return length;
0449 }
0450 EXPORT_SYMBOL(hdmi_audio_infoframe_pack_only);
0451 
0452 /**
0453  * hdmi_audio_infoframe_pack() - check a HDMI Audio infoframe,
0454  *                               and write it to binary buffer
0455  * @frame: HDMI Audio infoframe
0456  * @buffer: destination buffer
0457  * @size: size of buffer
0458  *
0459  * Validates that the infoframe is consistent and updates derived fields
0460  * (eg. length) based on other fields, after which it packs the information
0461  * contained in the @frame structure into a binary representation that
0462  * can be written into the corresponding controller registers. This function
0463  * also computes the checksum as required by section 5.3.5 of the HDMI 1.4
0464  * specification.
0465  *
0466  * Returns the number of bytes packed into the binary buffer or a negative
0467  * error code on failure.
0468  */
0469 ssize_t hdmi_audio_infoframe_pack(struct hdmi_audio_infoframe *frame,
0470                   void *buffer, size_t size)
0471 {
0472     int ret;
0473 
0474     ret = hdmi_audio_infoframe_check(frame);
0475     if (ret)
0476         return ret;
0477 
0478     return hdmi_audio_infoframe_pack_only(frame, buffer, size);
0479 }
0480 EXPORT_SYMBOL(hdmi_audio_infoframe_pack);
0481 
0482 /**
0483  * hdmi_vendor_infoframe_init() - initialize an HDMI vendor infoframe
0484  * @frame: HDMI vendor infoframe
0485  *
0486  * Returns 0 on success or a negative error code on failure.
0487  */
0488 int hdmi_vendor_infoframe_init(struct hdmi_vendor_infoframe *frame)
0489 {
0490     memset(frame, 0, sizeof(*frame));
0491 
0492     frame->type = HDMI_INFOFRAME_TYPE_VENDOR;
0493     frame->version = 1;
0494 
0495     frame->oui = HDMI_IEEE_OUI;
0496 
0497     /*
0498      * 0 is a valid value for s3d_struct, so we use a special "not set"
0499      * value
0500      */
0501     frame->s3d_struct = HDMI_3D_STRUCTURE_INVALID;
0502     frame->length = HDMI_VENDOR_INFOFRAME_SIZE;
0503 
0504     return 0;
0505 }
0506 EXPORT_SYMBOL(hdmi_vendor_infoframe_init);
0507 
0508 static int hdmi_vendor_infoframe_length(const struct hdmi_vendor_infoframe *frame)
0509 {
0510     /* for side by side (half) we also need to provide 3D_Ext_Data */
0511     if (frame->s3d_struct >= HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF)
0512         return 6;
0513     else if (frame->vic != 0 || frame->s3d_struct != HDMI_3D_STRUCTURE_INVALID)
0514         return 5;
0515     else
0516         return 4;
0517 }
0518 
0519 static int hdmi_vendor_infoframe_check_only(const struct hdmi_vendor_infoframe *frame)
0520 {
0521     if (frame->type != HDMI_INFOFRAME_TYPE_VENDOR ||
0522         frame->version != 1 ||
0523         frame->oui != HDMI_IEEE_OUI)
0524         return -EINVAL;
0525 
0526     /* only one of those can be supplied */
0527     if (frame->vic != 0 && frame->s3d_struct != HDMI_3D_STRUCTURE_INVALID)
0528         return -EINVAL;
0529 
0530     if (frame->length != hdmi_vendor_infoframe_length(frame))
0531         return -EINVAL;
0532 
0533     return 0;
0534 }
0535 
0536 /**
0537  * hdmi_vendor_infoframe_check() - check a HDMI vendor infoframe
0538  * @frame: HDMI infoframe
0539  *
0540  * Validates that the infoframe is consistent and updates derived fields
0541  * (eg. length) based on other fields.
0542  *
0543  * Returns 0 on success or a negative error code on failure.
0544  */
0545 int hdmi_vendor_infoframe_check(struct hdmi_vendor_infoframe *frame)
0546 {
0547     frame->length = hdmi_vendor_infoframe_length(frame);
0548 
0549     return hdmi_vendor_infoframe_check_only(frame);
0550 }
0551 EXPORT_SYMBOL(hdmi_vendor_infoframe_check);
0552 
0553 /**
0554  * hdmi_vendor_infoframe_pack_only() - write a HDMI vendor infoframe to binary buffer
0555  * @frame: HDMI infoframe
0556  * @buffer: destination buffer
0557  * @size: size of buffer
0558  *
0559  * Packs the information contained in the @frame structure into a binary
0560  * representation that can be written into the corresponding controller
0561  * registers. Also computes the checksum as required by section 5.3.5 of
0562  * the HDMI 1.4 specification.
0563  *
0564  * Returns the number of bytes packed into the binary buffer or a negative
0565  * error code on failure.
0566  */
0567 ssize_t hdmi_vendor_infoframe_pack_only(const struct hdmi_vendor_infoframe *frame,
0568                     void *buffer, size_t size)
0569 {
0570     u8 *ptr = buffer;
0571     size_t length;
0572     int ret;
0573 
0574     ret = hdmi_vendor_infoframe_check_only(frame);
0575     if (ret)
0576         return ret;
0577 
0578     length = HDMI_INFOFRAME_HEADER_SIZE + frame->length;
0579 
0580     if (size < length)
0581         return -ENOSPC;
0582 
0583     memset(buffer, 0, size);
0584 
0585     ptr[0] = frame->type;
0586     ptr[1] = frame->version;
0587     ptr[2] = frame->length;
0588     ptr[3] = 0; /* checksum */
0589 
0590     /* HDMI OUI */
0591     ptr[4] = 0x03;
0592     ptr[5] = 0x0c;
0593     ptr[6] = 0x00;
0594 
0595     if (frame->s3d_struct != HDMI_3D_STRUCTURE_INVALID) {
0596         ptr[7] = 0x2 << 5;  /* video format */
0597         ptr[8] = (frame->s3d_struct & 0xf) << 4;
0598         if (frame->s3d_struct >= HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF)
0599             ptr[9] = (frame->s3d_ext_data & 0xf) << 4;
0600     } else if (frame->vic) {
0601         ptr[7] = 0x1 << 5;  /* video format */
0602         ptr[8] = frame->vic;
0603     } else {
0604         ptr[7] = 0x0 << 5;  /* video format */
0605     }
0606 
0607     hdmi_infoframe_set_checksum(buffer, length);
0608 
0609     return length;
0610 }
0611 EXPORT_SYMBOL(hdmi_vendor_infoframe_pack_only);
0612 
0613 /**
0614  * hdmi_vendor_infoframe_pack() - check a HDMI Vendor infoframe,
0615  *                                and write it to binary buffer
0616  * @frame: HDMI Vendor infoframe
0617  * @buffer: destination buffer
0618  * @size: size of buffer
0619  *
0620  * Validates that the infoframe is consistent and updates derived fields
0621  * (eg. length) based on other fields, after which it packs the information
0622  * contained in the @frame structure into a binary representation that
0623  * can be written into the corresponding controller registers. This function
0624  * also computes the checksum as required by section 5.3.5 of the HDMI 1.4
0625  * specification.
0626  *
0627  * Returns the number of bytes packed into the binary buffer or a negative
0628  * error code on failure.
0629  */
0630 ssize_t hdmi_vendor_infoframe_pack(struct hdmi_vendor_infoframe *frame,
0631                    void *buffer, size_t size)
0632 {
0633     int ret;
0634 
0635     ret = hdmi_vendor_infoframe_check(frame);
0636     if (ret)
0637         return ret;
0638 
0639     return hdmi_vendor_infoframe_pack_only(frame, buffer, size);
0640 }
0641 EXPORT_SYMBOL(hdmi_vendor_infoframe_pack);
0642 
0643 static int
0644 hdmi_vendor_any_infoframe_check_only(const union hdmi_vendor_any_infoframe *frame)
0645 {
0646     if (frame->any.type != HDMI_INFOFRAME_TYPE_VENDOR ||
0647         frame->any.version != 1)
0648         return -EINVAL;
0649 
0650     return 0;
0651 }
0652 
0653 /**
0654  * hdmi_drm_infoframe_init() - initialize an HDMI Dynaminc Range and
0655  * mastering infoframe
0656  * @frame: HDMI DRM infoframe
0657  *
0658  * Returns 0 on success or a negative error code on failure.
0659  */
0660 int hdmi_drm_infoframe_init(struct hdmi_drm_infoframe *frame)
0661 {
0662     memset(frame, 0, sizeof(*frame));
0663 
0664     frame->type = HDMI_INFOFRAME_TYPE_DRM;
0665     frame->version = 1;
0666     frame->length = HDMI_DRM_INFOFRAME_SIZE;
0667 
0668     return 0;
0669 }
0670 EXPORT_SYMBOL(hdmi_drm_infoframe_init);
0671 
0672 static int hdmi_drm_infoframe_check_only(const struct hdmi_drm_infoframe *frame)
0673 {
0674     if (frame->type != HDMI_INFOFRAME_TYPE_DRM ||
0675         frame->version != 1)
0676         return -EINVAL;
0677 
0678     if (frame->length != HDMI_DRM_INFOFRAME_SIZE)
0679         return -EINVAL;
0680 
0681     return 0;
0682 }
0683 
0684 /**
0685  * hdmi_drm_infoframe_check() - check a HDMI DRM infoframe
0686  * @frame: HDMI DRM infoframe
0687  *
0688  * Validates that the infoframe is consistent.
0689  * Returns 0 on success or a negative error code on failure.
0690  */
0691 int hdmi_drm_infoframe_check(struct hdmi_drm_infoframe *frame)
0692 {
0693     return hdmi_drm_infoframe_check_only(frame);
0694 }
0695 EXPORT_SYMBOL(hdmi_drm_infoframe_check);
0696 
0697 /**
0698  * hdmi_drm_infoframe_pack_only() - write HDMI DRM infoframe to binary buffer
0699  * @frame: HDMI DRM infoframe
0700  * @buffer: destination buffer
0701  * @size: size of buffer
0702  *
0703  * Packs the information contained in the @frame structure into a binary
0704  * representation that can be written into the corresponding controller
0705  * registers. Also computes the checksum as required by section 5.3.5 of
0706  * the HDMI 1.4 specification.
0707  *
0708  * Returns the number of bytes packed into the binary buffer or a negative
0709  * error code on failure.
0710  */
0711 ssize_t hdmi_drm_infoframe_pack_only(const struct hdmi_drm_infoframe *frame,
0712                      void *buffer, size_t size)
0713 {
0714     u8 *ptr = buffer;
0715     size_t length;
0716     int i;
0717 
0718     length = HDMI_INFOFRAME_HEADER_SIZE + frame->length;
0719 
0720     if (size < length)
0721         return -ENOSPC;
0722 
0723     memset(buffer, 0, size);
0724 
0725     ptr[0] = frame->type;
0726     ptr[1] = frame->version;
0727     ptr[2] = frame->length;
0728     ptr[3] = 0; /* checksum */
0729 
0730     /* start infoframe payload */
0731     ptr += HDMI_INFOFRAME_HEADER_SIZE;
0732 
0733     *ptr++ = frame->eotf;
0734     *ptr++ = frame->metadata_type;
0735 
0736     for (i = 0; i < 3; i++) {
0737         *ptr++ = frame->display_primaries[i].x;
0738         *ptr++ = frame->display_primaries[i].x >> 8;
0739         *ptr++ = frame->display_primaries[i].y;
0740         *ptr++ = frame->display_primaries[i].y >> 8;
0741     }
0742 
0743     *ptr++ = frame->white_point.x;
0744     *ptr++ = frame->white_point.x >> 8;
0745 
0746     *ptr++ = frame->white_point.y;
0747     *ptr++ = frame->white_point.y >> 8;
0748 
0749     *ptr++ = frame->max_display_mastering_luminance;
0750     *ptr++ = frame->max_display_mastering_luminance >> 8;
0751 
0752     *ptr++ = frame->min_display_mastering_luminance;
0753     *ptr++ = frame->min_display_mastering_luminance >> 8;
0754 
0755     *ptr++ = frame->max_cll;
0756     *ptr++ = frame->max_cll >> 8;
0757 
0758     *ptr++ = frame->max_fall;
0759     *ptr++ = frame->max_fall >> 8;
0760 
0761     hdmi_infoframe_set_checksum(buffer, length);
0762 
0763     return length;
0764 }
0765 EXPORT_SYMBOL(hdmi_drm_infoframe_pack_only);
0766 
0767 /**
0768  * hdmi_drm_infoframe_pack() - check a HDMI DRM infoframe,
0769  *                             and write it to binary buffer
0770  * @frame: HDMI DRM infoframe
0771  * @buffer: destination buffer
0772  * @size: size of buffer
0773  *
0774  * Validates that the infoframe is consistent and updates derived fields
0775  * (eg. length) based on other fields, after which it packs the information
0776  * contained in the @frame structure into a binary representation that
0777  * can be written into the corresponding controller registers. This function
0778  * also computes the checksum as required by section 5.3.5 of the HDMI 1.4
0779  * specification.
0780  *
0781  * Returns the number of bytes packed into the binary buffer or a negative
0782  * error code on failure.
0783  */
0784 ssize_t hdmi_drm_infoframe_pack(struct hdmi_drm_infoframe *frame,
0785                 void *buffer, size_t size)
0786 {
0787     int ret;
0788 
0789     ret = hdmi_drm_infoframe_check(frame);
0790     if (ret)
0791         return ret;
0792 
0793     return hdmi_drm_infoframe_pack_only(frame, buffer, size);
0794 }
0795 EXPORT_SYMBOL(hdmi_drm_infoframe_pack);
0796 
0797 /*
0798  * hdmi_vendor_any_infoframe_check() - check a vendor infoframe
0799  */
0800 static int
0801 hdmi_vendor_any_infoframe_check(union hdmi_vendor_any_infoframe *frame)
0802 {
0803     int ret;
0804 
0805     ret = hdmi_vendor_any_infoframe_check_only(frame);
0806     if (ret)
0807         return ret;
0808 
0809     /* we only know about HDMI vendor infoframes */
0810     if (frame->any.oui != HDMI_IEEE_OUI)
0811         return -EINVAL;
0812 
0813     return hdmi_vendor_infoframe_check(&frame->hdmi);
0814 }
0815 
0816 /*
0817  * hdmi_vendor_any_infoframe_pack_only() - write a vendor infoframe to binary buffer
0818  */
0819 static ssize_t
0820 hdmi_vendor_any_infoframe_pack_only(const union hdmi_vendor_any_infoframe *frame,
0821                     void *buffer, size_t size)
0822 {
0823     int ret;
0824 
0825     ret = hdmi_vendor_any_infoframe_check_only(frame);
0826     if (ret)
0827         return ret;
0828 
0829     /* we only know about HDMI vendor infoframes */
0830     if (frame->any.oui != HDMI_IEEE_OUI)
0831         return -EINVAL;
0832 
0833     return hdmi_vendor_infoframe_pack_only(&frame->hdmi, buffer, size);
0834 }
0835 
0836 /*
0837  * hdmi_vendor_any_infoframe_pack() - check a vendor infoframe,
0838  *                                    and write it to binary buffer
0839  */
0840 static ssize_t
0841 hdmi_vendor_any_infoframe_pack(union hdmi_vendor_any_infoframe *frame,
0842                    void *buffer, size_t size)
0843 {
0844     int ret;
0845 
0846     ret = hdmi_vendor_any_infoframe_check(frame);
0847     if (ret)
0848         return ret;
0849 
0850     return hdmi_vendor_any_infoframe_pack_only(frame, buffer, size);
0851 }
0852 
0853 /**
0854  * hdmi_infoframe_check() - check a HDMI infoframe
0855  * @frame: HDMI infoframe
0856  *
0857  * Validates that the infoframe is consistent and updates derived fields
0858  * (eg. length) based on other fields.
0859  *
0860  * Returns 0 on success or a negative error code on failure.
0861  */
0862 int
0863 hdmi_infoframe_check(union hdmi_infoframe *frame)
0864 {
0865     switch (frame->any.type) {
0866     case HDMI_INFOFRAME_TYPE_AVI:
0867         return hdmi_avi_infoframe_check(&frame->avi);
0868     case HDMI_INFOFRAME_TYPE_SPD:
0869         return hdmi_spd_infoframe_check(&frame->spd);
0870     case HDMI_INFOFRAME_TYPE_AUDIO:
0871         return hdmi_audio_infoframe_check(&frame->audio);
0872     case HDMI_INFOFRAME_TYPE_VENDOR:
0873         return hdmi_vendor_any_infoframe_check(&frame->vendor);
0874     default:
0875         WARN(1, "Bad infoframe type %d\n", frame->any.type);
0876         return -EINVAL;
0877     }
0878 }
0879 EXPORT_SYMBOL(hdmi_infoframe_check);
0880 
0881 /**
0882  * hdmi_infoframe_pack_only() - write a HDMI infoframe to binary buffer
0883  * @frame: HDMI infoframe
0884  * @buffer: destination buffer
0885  * @size: size of buffer
0886  *
0887  * Packs the information contained in the @frame structure into a binary
0888  * representation that can be written into the corresponding controller
0889  * registers. Also computes the checksum as required by section 5.3.5 of
0890  * the HDMI 1.4 specification.
0891  *
0892  * Returns the number of bytes packed into the binary buffer or a negative
0893  * error code on failure.
0894  */
0895 ssize_t
0896 hdmi_infoframe_pack_only(const union hdmi_infoframe *frame, void *buffer, size_t size)
0897 {
0898     ssize_t length;
0899 
0900     switch (frame->any.type) {
0901     case HDMI_INFOFRAME_TYPE_AVI:
0902         length = hdmi_avi_infoframe_pack_only(&frame->avi,
0903                               buffer, size);
0904         break;
0905     case HDMI_INFOFRAME_TYPE_DRM:
0906         length = hdmi_drm_infoframe_pack_only(&frame->drm,
0907                               buffer, size);
0908         break;
0909     case HDMI_INFOFRAME_TYPE_SPD:
0910         length = hdmi_spd_infoframe_pack_only(&frame->spd,
0911                               buffer, size);
0912         break;
0913     case HDMI_INFOFRAME_TYPE_AUDIO:
0914         length = hdmi_audio_infoframe_pack_only(&frame->audio,
0915                             buffer, size);
0916         break;
0917     case HDMI_INFOFRAME_TYPE_VENDOR:
0918         length = hdmi_vendor_any_infoframe_pack_only(&frame->vendor,
0919                                  buffer, size);
0920         break;
0921     default:
0922         WARN(1, "Bad infoframe type %d\n", frame->any.type);
0923         length = -EINVAL;
0924     }
0925 
0926     return length;
0927 }
0928 EXPORT_SYMBOL(hdmi_infoframe_pack_only);
0929 
0930 /**
0931  * hdmi_infoframe_pack() - check a HDMI infoframe,
0932  *                         and write it to binary buffer
0933  * @frame: HDMI infoframe
0934  * @buffer: destination buffer
0935  * @size: size of buffer
0936  *
0937  * Validates that the infoframe is consistent and updates derived fields
0938  * (eg. length) based on other fields, after which it packs the information
0939  * contained in the @frame structure into a binary representation that
0940  * can be written into the corresponding controller registers. This function
0941  * also computes the checksum as required by section 5.3.5 of the HDMI 1.4
0942  * specification.
0943  *
0944  * Returns the number of bytes packed into the binary buffer or a negative
0945  * error code on failure.
0946  */
0947 ssize_t
0948 hdmi_infoframe_pack(union hdmi_infoframe *frame,
0949             void *buffer, size_t size)
0950 {
0951     ssize_t length;
0952 
0953     switch (frame->any.type) {
0954     case HDMI_INFOFRAME_TYPE_AVI:
0955         length = hdmi_avi_infoframe_pack(&frame->avi, buffer, size);
0956         break;
0957     case HDMI_INFOFRAME_TYPE_DRM:
0958         length = hdmi_drm_infoframe_pack(&frame->drm, buffer, size);
0959         break;
0960     case HDMI_INFOFRAME_TYPE_SPD:
0961         length = hdmi_spd_infoframe_pack(&frame->spd, buffer, size);
0962         break;
0963     case HDMI_INFOFRAME_TYPE_AUDIO:
0964         length = hdmi_audio_infoframe_pack(&frame->audio, buffer, size);
0965         break;
0966     case HDMI_INFOFRAME_TYPE_VENDOR:
0967         length = hdmi_vendor_any_infoframe_pack(&frame->vendor,
0968                             buffer, size);
0969         break;
0970     default:
0971         WARN(1, "Bad infoframe type %d\n", frame->any.type);
0972         length = -EINVAL;
0973     }
0974 
0975     return length;
0976 }
0977 EXPORT_SYMBOL(hdmi_infoframe_pack);
0978 
0979 static const char *hdmi_infoframe_type_get_name(enum hdmi_infoframe_type type)
0980 {
0981     if (type < 0x80 || type > 0x9f)
0982         return "Invalid";
0983     switch (type) {
0984     case HDMI_INFOFRAME_TYPE_VENDOR:
0985         return "Vendor";
0986     case HDMI_INFOFRAME_TYPE_AVI:
0987         return "Auxiliary Video Information (AVI)";
0988     case HDMI_INFOFRAME_TYPE_SPD:
0989         return "Source Product Description (SPD)";
0990     case HDMI_INFOFRAME_TYPE_AUDIO:
0991         return "Audio";
0992     case HDMI_INFOFRAME_TYPE_DRM:
0993         return "Dynamic Range and Mastering";
0994     }
0995     return "Reserved";
0996 }
0997 
0998 static void hdmi_infoframe_log_header(const char *level,
0999                       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 static void hdmi_avi_infoframe_log(const char *level,
1195                    struct device *dev,
1196                    const struct hdmi_avi_infoframe *frame)
1197 {
1198     hdmi_infoframe_log_header(level, dev,
1199                   (const struct hdmi_any_infoframe *)frame);
1200 
1201     hdmi_log("    colorspace: %s\n",
1202             hdmi_colorspace_get_name(frame->colorspace));
1203     hdmi_log("    scan mode: %s\n",
1204             hdmi_scan_mode_get_name(frame->scan_mode));
1205     hdmi_log("    colorimetry: %s\n",
1206             hdmi_colorimetry_get_name(frame->colorimetry));
1207     hdmi_log("    picture aspect: %s\n",
1208             hdmi_picture_aspect_get_name(frame->picture_aspect));
1209     hdmi_log("    active aspect: %s\n",
1210             hdmi_active_aspect_get_name(frame->active_aspect));
1211     hdmi_log("    itc: %s\n", frame->itc ? "IT Content" : "No Data");
1212     hdmi_log("    extended colorimetry: %s\n",
1213             hdmi_extended_colorimetry_get_name(frame->extended_colorimetry));
1214     hdmi_log("    quantization range: %s\n",
1215             hdmi_quantization_range_get_name(frame->quantization_range));
1216     hdmi_log("    nups: %s\n", hdmi_nups_get_name(frame->nups));
1217     hdmi_log("    video code: %u\n", frame->video_code);
1218     hdmi_log("    ycc quantization range: %s\n",
1219             hdmi_ycc_quantization_range_get_name(frame->ycc_quantization_range));
1220     hdmi_log("    hdmi content type: %s\n",
1221             hdmi_content_type_get_name(frame->content_type));
1222     hdmi_log("    pixel repeat: %u\n", frame->pixel_repeat);
1223     hdmi_log("    bar top %u, bottom %u, left %u, right %u\n",
1224             frame->top_bar, frame->bottom_bar,
1225             frame->left_bar, frame->right_bar);
1226 }
1227 
1228 static const char *hdmi_spd_sdi_get_name(enum hdmi_spd_sdi sdi)
1229 {
1230     if (sdi < 0 || sdi > 0xff)
1231         return "Invalid";
1232     switch (sdi) {
1233     case HDMI_SPD_SDI_UNKNOWN:
1234         return "Unknown";
1235     case HDMI_SPD_SDI_DSTB:
1236         return "Digital STB";
1237     case HDMI_SPD_SDI_DVDP:
1238         return "DVD Player";
1239     case HDMI_SPD_SDI_DVHS:
1240         return "D-VHS";
1241     case HDMI_SPD_SDI_HDDVR:
1242         return "HDD Videorecorder";
1243     case HDMI_SPD_SDI_DVC:
1244         return "DVC";
1245     case HDMI_SPD_SDI_DSC:
1246         return "DSC";
1247     case HDMI_SPD_SDI_VCD:
1248         return "Video CD";
1249     case HDMI_SPD_SDI_GAME:
1250         return "Game";
1251     case HDMI_SPD_SDI_PC:
1252         return "PC General";
1253     case HDMI_SPD_SDI_BD:
1254         return "Blu-Ray Disc (BD)";
1255     case HDMI_SPD_SDI_SACD:
1256         return "Super Audio CD";
1257     case HDMI_SPD_SDI_HDDVD:
1258         return "HD DVD";
1259     case HDMI_SPD_SDI_PMP:
1260         return "PMP";
1261     }
1262     return "Reserved";
1263 }
1264 
1265 static void hdmi_spd_infoframe_log(const char *level,
1266                    struct device *dev,
1267                    const struct hdmi_spd_infoframe *frame)
1268 {
1269     u8 buf[17];
1270 
1271     hdmi_infoframe_log_header(level, dev,
1272                   (const struct hdmi_any_infoframe *)frame);
1273 
1274     memset(buf, 0, sizeof(buf));
1275 
1276     strncpy(buf, frame->vendor, 8);
1277     hdmi_log("    vendor: %s\n", buf);
1278     strncpy(buf, frame->product, 16);
1279     hdmi_log("    product: %s\n", buf);
1280     hdmi_log("    source device information: %s (0x%x)\n",
1281         hdmi_spd_sdi_get_name(frame->sdi), frame->sdi);
1282 }
1283 
1284 static const char *
1285 hdmi_audio_coding_type_get_name(enum hdmi_audio_coding_type coding_type)
1286 {
1287     switch (coding_type) {
1288     case HDMI_AUDIO_CODING_TYPE_STREAM:
1289         return "Refer to Stream Header";
1290     case HDMI_AUDIO_CODING_TYPE_PCM:
1291         return "PCM";
1292     case HDMI_AUDIO_CODING_TYPE_AC3:
1293         return "AC-3";
1294     case HDMI_AUDIO_CODING_TYPE_MPEG1:
1295         return "MPEG1";
1296     case HDMI_AUDIO_CODING_TYPE_MP3:
1297         return "MP3";
1298     case HDMI_AUDIO_CODING_TYPE_MPEG2:
1299         return "MPEG2";
1300     case HDMI_AUDIO_CODING_TYPE_AAC_LC:
1301         return "AAC";
1302     case HDMI_AUDIO_CODING_TYPE_DTS:
1303         return "DTS";
1304     case HDMI_AUDIO_CODING_TYPE_ATRAC:
1305         return "ATRAC";
1306     case HDMI_AUDIO_CODING_TYPE_DSD:
1307         return "One Bit Audio";
1308     case HDMI_AUDIO_CODING_TYPE_EAC3:
1309         return "Dolby Digital +";
1310     case HDMI_AUDIO_CODING_TYPE_DTS_HD:
1311         return "DTS-HD";
1312     case HDMI_AUDIO_CODING_TYPE_MLP:
1313         return "MAT (MLP)";
1314     case HDMI_AUDIO_CODING_TYPE_DST:
1315         return "DST";
1316     case HDMI_AUDIO_CODING_TYPE_WMA_PRO:
1317         return "WMA PRO";
1318     case HDMI_AUDIO_CODING_TYPE_CXT:
1319         return "Refer to CXT";
1320     }
1321     return "Invalid";
1322 }
1323 
1324 static const char *
1325 hdmi_audio_sample_size_get_name(enum hdmi_audio_sample_size sample_size)
1326 {
1327     switch (sample_size) {
1328     case HDMI_AUDIO_SAMPLE_SIZE_STREAM:
1329         return "Refer to Stream Header";
1330     case HDMI_AUDIO_SAMPLE_SIZE_16:
1331         return "16 bit";
1332     case HDMI_AUDIO_SAMPLE_SIZE_20:
1333         return "20 bit";
1334     case HDMI_AUDIO_SAMPLE_SIZE_24:
1335         return "24 bit";
1336     }
1337     return "Invalid";
1338 }
1339 
1340 static const char *
1341 hdmi_audio_sample_frequency_get_name(enum hdmi_audio_sample_frequency freq)
1342 {
1343     switch (freq) {
1344     case HDMI_AUDIO_SAMPLE_FREQUENCY_STREAM:
1345         return "Refer to Stream Header";
1346     case HDMI_AUDIO_SAMPLE_FREQUENCY_32000:
1347         return "32 kHz";
1348     case HDMI_AUDIO_SAMPLE_FREQUENCY_44100:
1349         return "44.1 kHz (CD)";
1350     case HDMI_AUDIO_SAMPLE_FREQUENCY_48000:
1351         return "48 kHz";
1352     case HDMI_AUDIO_SAMPLE_FREQUENCY_88200:
1353         return "88.2 kHz";
1354     case HDMI_AUDIO_SAMPLE_FREQUENCY_96000:
1355         return "96 kHz";
1356     case HDMI_AUDIO_SAMPLE_FREQUENCY_176400:
1357         return "176.4 kHz";
1358     case HDMI_AUDIO_SAMPLE_FREQUENCY_192000:
1359         return "192 kHz";
1360     }
1361     return "Invalid";
1362 }
1363 
1364 static const char *
1365 hdmi_audio_coding_type_ext_get_name(enum hdmi_audio_coding_type_ext ctx)
1366 {
1367     if (ctx < 0 || ctx > 0x1f)
1368         return "Invalid";
1369 
1370     switch (ctx) {
1371     case HDMI_AUDIO_CODING_TYPE_EXT_CT:
1372         return "Refer to CT";
1373     case HDMI_AUDIO_CODING_TYPE_EXT_HE_AAC:
1374         return "HE AAC";
1375     case HDMI_AUDIO_CODING_TYPE_EXT_HE_AAC_V2:
1376         return "HE AAC v2";
1377     case HDMI_AUDIO_CODING_TYPE_EXT_MPEG_SURROUND:
1378         return "MPEG SURROUND";
1379     case HDMI_AUDIO_CODING_TYPE_EXT_MPEG4_HE_AAC:
1380         return "MPEG-4 HE AAC";
1381     case HDMI_AUDIO_CODING_TYPE_EXT_MPEG4_HE_AAC_V2:
1382         return "MPEG-4 HE AAC v2";
1383     case HDMI_AUDIO_CODING_TYPE_EXT_MPEG4_AAC_LC:
1384         return "MPEG-4 AAC LC";
1385     case HDMI_AUDIO_CODING_TYPE_EXT_DRA:
1386         return "DRA";
1387     case HDMI_AUDIO_CODING_TYPE_EXT_MPEG4_HE_AAC_SURROUND:
1388         return "MPEG-4 HE AAC + MPEG Surround";
1389     case HDMI_AUDIO_CODING_TYPE_EXT_MPEG4_AAC_LC_SURROUND:
1390         return "MPEG-4 AAC LC + MPEG Surround";
1391     }
1392     return "Reserved";
1393 }
1394 
1395 static void hdmi_audio_infoframe_log(const char *level,
1396                      struct device *dev,
1397                      const struct hdmi_audio_infoframe *frame)
1398 {
1399     hdmi_infoframe_log_header(level, dev,
1400                   (const struct hdmi_any_infoframe *)frame);
1401 
1402     if (frame->channels)
1403         hdmi_log("    channels: %u\n", frame->channels - 1);
1404     else
1405         hdmi_log("    channels: Refer to stream header\n");
1406     hdmi_log("    coding type: %s\n",
1407             hdmi_audio_coding_type_get_name(frame->coding_type));
1408     hdmi_log("    sample size: %s\n",
1409             hdmi_audio_sample_size_get_name(frame->sample_size));
1410     hdmi_log("    sample frequency: %s\n",
1411             hdmi_audio_sample_frequency_get_name(frame->sample_frequency));
1412     hdmi_log("    coding type ext: %s\n",
1413             hdmi_audio_coding_type_ext_get_name(frame->coding_type_ext));
1414     hdmi_log("    channel allocation: 0x%x\n",
1415             frame->channel_allocation);
1416     hdmi_log("    level shift value: %u dB\n",
1417             frame->level_shift_value);
1418     hdmi_log("    downmix inhibit: %s\n",
1419             frame->downmix_inhibit ? "Yes" : "No");
1420 }
1421 
1422 static void hdmi_drm_infoframe_log(const char *level,
1423                    struct device *dev,
1424                    const struct hdmi_drm_infoframe *frame)
1425 {
1426     int i;
1427 
1428     hdmi_infoframe_log_header(level, dev,
1429                   (struct hdmi_any_infoframe *)frame);
1430     hdmi_log("length: %d\n", frame->length);
1431     hdmi_log("metadata type: %d\n", frame->metadata_type);
1432     hdmi_log("eotf: %d\n", frame->eotf);
1433     for (i = 0; i < 3; i++) {
1434         hdmi_log("x[%d]: %d\n", i, frame->display_primaries[i].x);
1435         hdmi_log("y[%d]: %d\n", i, frame->display_primaries[i].y);
1436     }
1437 
1438     hdmi_log("white point x: %d\n", frame->white_point.x);
1439     hdmi_log("white point y: %d\n", frame->white_point.y);
1440 
1441     hdmi_log("max_display_mastering_luminance: %d\n",
1442          frame->max_display_mastering_luminance);
1443     hdmi_log("min_display_mastering_luminance: %d\n",
1444          frame->min_display_mastering_luminance);
1445 
1446     hdmi_log("max_cll: %d\n", frame->max_cll);
1447     hdmi_log("max_fall: %d\n", frame->max_fall);
1448 }
1449 
1450 static const char *
1451 hdmi_3d_structure_get_name(enum hdmi_3d_structure s3d_struct)
1452 {
1453     if (s3d_struct < 0 || s3d_struct > 0xf)
1454         return "Invalid";
1455 
1456     switch (s3d_struct) {
1457     case HDMI_3D_STRUCTURE_FRAME_PACKING:
1458         return "Frame Packing";
1459     case HDMI_3D_STRUCTURE_FIELD_ALTERNATIVE:
1460         return "Field Alternative";
1461     case HDMI_3D_STRUCTURE_LINE_ALTERNATIVE:
1462         return "Line Alternative";
1463     case HDMI_3D_STRUCTURE_SIDE_BY_SIDE_FULL:
1464         return "Side-by-side (Full)";
1465     case HDMI_3D_STRUCTURE_L_DEPTH:
1466         return "L + Depth";
1467     case HDMI_3D_STRUCTURE_L_DEPTH_GFX_GFX_DEPTH:
1468         return "L + Depth + Graphics + Graphics-depth";
1469     case HDMI_3D_STRUCTURE_TOP_AND_BOTTOM:
1470         return "Top-and-Bottom";
1471     case HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF:
1472         return "Side-by-side (Half)";
1473     default:
1474         break;
1475     }
1476     return "Reserved";
1477 }
1478 
1479 static void
1480 hdmi_vendor_any_infoframe_log(const char *level,
1481                   struct device *dev,
1482                   const union hdmi_vendor_any_infoframe *frame)
1483 {
1484     const struct hdmi_vendor_infoframe *hvf = &frame->hdmi;
1485 
1486     hdmi_infoframe_log_header(level, dev,
1487                   (const struct hdmi_any_infoframe *)frame);
1488 
1489     if (frame->any.oui != HDMI_IEEE_OUI) {
1490         hdmi_log("    not a HDMI vendor infoframe\n");
1491         return;
1492     }
1493     if (hvf->vic == 0 && hvf->s3d_struct == HDMI_3D_STRUCTURE_INVALID) {
1494         hdmi_log("    empty frame\n");
1495         return;
1496     }
1497 
1498     if (hvf->vic)
1499         hdmi_log("    HDMI VIC: %u\n", hvf->vic);
1500     if (hvf->s3d_struct != HDMI_3D_STRUCTURE_INVALID) {
1501         hdmi_log("    3D structure: %s\n",
1502                 hdmi_3d_structure_get_name(hvf->s3d_struct));
1503         if (hvf->s3d_struct >= HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF)
1504             hdmi_log("    3D extension data: %d\n",
1505                     hvf->s3d_ext_data);
1506     }
1507 }
1508 
1509 /**
1510  * hdmi_infoframe_log() - log info of HDMI infoframe
1511  * @level: logging level
1512  * @dev: device
1513  * @frame: HDMI infoframe
1514  */
1515 void hdmi_infoframe_log(const char *level,
1516             struct device *dev,
1517             const union hdmi_infoframe *frame)
1518 {
1519     switch (frame->any.type) {
1520     case HDMI_INFOFRAME_TYPE_AVI:
1521         hdmi_avi_infoframe_log(level, dev, &frame->avi);
1522         break;
1523     case HDMI_INFOFRAME_TYPE_SPD:
1524         hdmi_spd_infoframe_log(level, dev, &frame->spd);
1525         break;
1526     case HDMI_INFOFRAME_TYPE_AUDIO:
1527         hdmi_audio_infoframe_log(level, dev, &frame->audio);
1528         break;
1529     case HDMI_INFOFRAME_TYPE_VENDOR:
1530         hdmi_vendor_any_infoframe_log(level, dev, &frame->vendor);
1531         break;
1532     case HDMI_INFOFRAME_TYPE_DRM:
1533         hdmi_drm_infoframe_log(level, dev, &frame->drm);
1534         break;
1535     }
1536 }
1537 EXPORT_SYMBOL(hdmi_infoframe_log);
1538 
1539 /**
1540  * hdmi_avi_infoframe_unpack() - unpack binary buffer to a HDMI AVI infoframe
1541  * @frame: HDMI AVI infoframe
1542  * @buffer: source buffer
1543  * @size: size of buffer
1544  *
1545  * Unpacks the information contained in binary @buffer into a structured
1546  * @frame of the HDMI Auxiliary Video (AVI) information frame.
1547  * Also verifies the checksum as required by section 5.3.5 of the HDMI 1.4
1548  * specification.
1549  *
1550  * Returns 0 on success or a negative error code on failure.
1551  */
1552 static int hdmi_avi_infoframe_unpack(struct hdmi_avi_infoframe *frame,
1553                      const void *buffer, size_t size)
1554 {
1555     const u8 *ptr = buffer;
1556 
1557     if (size < HDMI_INFOFRAME_SIZE(AVI))
1558         return -EINVAL;
1559 
1560     if (ptr[0] != HDMI_INFOFRAME_TYPE_AVI ||
1561         ptr[1] != 2 ||
1562         ptr[2] != HDMI_AVI_INFOFRAME_SIZE)
1563         return -EINVAL;
1564 
1565     if (hdmi_infoframe_checksum(buffer, HDMI_INFOFRAME_SIZE(AVI)) != 0)
1566         return -EINVAL;
1567 
1568     hdmi_avi_infoframe_init(frame);
1569 
1570     ptr += HDMI_INFOFRAME_HEADER_SIZE;
1571 
1572     frame->colorspace = (ptr[0] >> 5) & 0x3;
1573     if (ptr[0] & 0x10)
1574         frame->active_aspect = ptr[1] & 0xf;
1575     if (ptr[0] & 0x8) {
1576         frame->top_bar = (ptr[6] << 8) | ptr[5];
1577         frame->bottom_bar = (ptr[8] << 8) | ptr[7];
1578     }
1579     if (ptr[0] & 0x4) {
1580         frame->left_bar = (ptr[10] << 8) | ptr[9];
1581         frame->right_bar = (ptr[12] << 8) | ptr[11];
1582     }
1583     frame->scan_mode = ptr[0] & 0x3;
1584 
1585     frame->colorimetry = (ptr[1] >> 6) & 0x3;
1586     frame->picture_aspect = (ptr[1] >> 4) & 0x3;
1587     frame->active_aspect = ptr[1] & 0xf;
1588 
1589     frame->itc = ptr[2] & 0x80 ? true : false;
1590     frame->extended_colorimetry = (ptr[2] >> 4) & 0x7;
1591     frame->quantization_range = (ptr[2] >> 2) & 0x3;
1592     frame->nups = ptr[2] & 0x3;
1593 
1594     frame->video_code = ptr[3] & 0x7f;
1595     frame->ycc_quantization_range = (ptr[4] >> 6) & 0x3;
1596     frame->content_type = (ptr[4] >> 4) & 0x3;
1597 
1598     frame->pixel_repeat = ptr[4] & 0xf;
1599 
1600     return 0;
1601 }
1602 
1603 /**
1604  * hdmi_spd_infoframe_unpack() - unpack binary buffer to a HDMI SPD infoframe
1605  * @frame: HDMI SPD infoframe
1606  * @buffer: source buffer
1607  * @size: size of buffer
1608  *
1609  * Unpacks the information contained in binary @buffer into a structured
1610  * @frame of the HDMI Source Product Description (SPD) information frame.
1611  * Also verifies the checksum as required by section 5.3.5 of the HDMI 1.4
1612  * specification.
1613  *
1614  * Returns 0 on success or a negative error code on failure.
1615  */
1616 static int hdmi_spd_infoframe_unpack(struct hdmi_spd_infoframe *frame,
1617                      const void *buffer, size_t size)
1618 {
1619     const u8 *ptr = buffer;
1620     int ret;
1621 
1622     if (size < HDMI_INFOFRAME_SIZE(SPD))
1623         return -EINVAL;
1624 
1625     if (ptr[0] != HDMI_INFOFRAME_TYPE_SPD ||
1626         ptr[1] != 1 ||
1627         ptr[2] != HDMI_SPD_INFOFRAME_SIZE) {
1628         return -EINVAL;
1629     }
1630 
1631     if (hdmi_infoframe_checksum(buffer, HDMI_INFOFRAME_SIZE(SPD)) != 0)
1632         return -EINVAL;
1633 
1634     ptr += HDMI_INFOFRAME_HEADER_SIZE;
1635 
1636     ret = hdmi_spd_infoframe_init(frame, ptr, ptr + 8);
1637     if (ret)
1638         return ret;
1639 
1640     frame->sdi = ptr[24];
1641 
1642     return 0;
1643 }
1644 
1645 /**
1646  * hdmi_audio_infoframe_unpack() - unpack binary buffer to a HDMI AUDIO infoframe
1647  * @frame: HDMI Audio infoframe
1648  * @buffer: source buffer
1649  * @size: size of buffer
1650  *
1651  * Unpacks the information contained in binary @buffer into a structured
1652  * @frame of the HDMI Audio information frame.
1653  * Also verifies the checksum as required by section 5.3.5 of the HDMI 1.4
1654  * specification.
1655  *
1656  * Returns 0 on success or a negative error code on failure.
1657  */
1658 static int hdmi_audio_infoframe_unpack(struct hdmi_audio_infoframe *frame,
1659                        const void *buffer, size_t size)
1660 {
1661     const u8 *ptr = buffer;
1662     int ret;
1663 
1664     if (size < HDMI_INFOFRAME_SIZE(AUDIO))
1665         return -EINVAL;
1666 
1667     if (ptr[0] != HDMI_INFOFRAME_TYPE_AUDIO ||
1668         ptr[1] != 1 ||
1669         ptr[2] != HDMI_AUDIO_INFOFRAME_SIZE) {
1670         return -EINVAL;
1671     }
1672 
1673     if (hdmi_infoframe_checksum(buffer, HDMI_INFOFRAME_SIZE(AUDIO)) != 0)
1674         return -EINVAL;
1675 
1676     ret = hdmi_audio_infoframe_init(frame);
1677     if (ret)
1678         return ret;
1679 
1680     ptr += HDMI_INFOFRAME_HEADER_SIZE;
1681 
1682     frame->channels = ptr[0] & 0x7;
1683     frame->coding_type = (ptr[0] >> 4) & 0xf;
1684     frame->sample_size = ptr[1] & 0x3;
1685     frame->sample_frequency = (ptr[1] >> 2) & 0x7;
1686     frame->coding_type_ext = ptr[2] & 0x1f;
1687     frame->channel_allocation = ptr[3];
1688     frame->level_shift_value = (ptr[4] >> 3) & 0xf;
1689     frame->downmix_inhibit = ptr[4] & 0x80 ? true : false;
1690 
1691     return 0;
1692 }
1693 
1694 /**
1695  * hdmi_vendor_any_infoframe_unpack() - unpack binary buffer to a HDMI
1696  *  vendor infoframe
1697  * @frame: HDMI Vendor infoframe
1698  * @buffer: source buffer
1699  * @size: size of buffer
1700  *
1701  * Unpacks the information contained in binary @buffer into a structured
1702  * @frame of the HDMI Vendor information frame.
1703  * Also verifies the checksum as required by section 5.3.5 of the HDMI 1.4
1704  * specification.
1705  *
1706  * Returns 0 on success or a negative error code on failure.
1707  */
1708 static int
1709 hdmi_vendor_any_infoframe_unpack(union hdmi_vendor_any_infoframe *frame,
1710                  const void *buffer, size_t size)
1711 {
1712     const u8 *ptr = buffer;
1713     size_t length;
1714     int ret;
1715     u8 hdmi_video_format;
1716     struct hdmi_vendor_infoframe *hvf = &frame->hdmi;
1717 
1718     if (size < HDMI_INFOFRAME_HEADER_SIZE)
1719         return -EINVAL;
1720 
1721     if (ptr[0] != HDMI_INFOFRAME_TYPE_VENDOR ||
1722         ptr[1] != 1 ||
1723         (ptr[2] != 4 && ptr[2] != 5 && ptr[2] != 6))
1724         return -EINVAL;
1725 
1726     length = ptr[2];
1727 
1728     if (size < HDMI_INFOFRAME_HEADER_SIZE + length)
1729         return -EINVAL;
1730 
1731     if (hdmi_infoframe_checksum(buffer,
1732                     HDMI_INFOFRAME_HEADER_SIZE + length) != 0)
1733         return -EINVAL;
1734 
1735     ptr += HDMI_INFOFRAME_HEADER_SIZE;
1736 
1737     /* HDMI OUI */
1738     if ((ptr[0] != 0x03) ||
1739         (ptr[1] != 0x0c) ||
1740         (ptr[2] != 0x00))
1741         return -EINVAL;
1742 
1743     hdmi_video_format = ptr[3] >> 5;
1744 
1745     if (hdmi_video_format > 0x2)
1746         return -EINVAL;
1747 
1748     ret = hdmi_vendor_infoframe_init(hvf);
1749     if (ret)
1750         return ret;
1751 
1752     hvf->length = length;
1753 
1754     if (hdmi_video_format == 0x2) {
1755         if (length != 5 && length != 6)
1756             return -EINVAL;
1757         hvf->s3d_struct = ptr[4] >> 4;
1758         if (hvf->s3d_struct >= HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF) {
1759             if (length != 6)
1760                 return -EINVAL;
1761             hvf->s3d_ext_data = ptr[5] >> 4;
1762         }
1763     } else if (hdmi_video_format == 0x1) {
1764         if (length != 5)
1765             return -EINVAL;
1766         hvf->vic = ptr[4];
1767     } else {
1768         if (length != 4)
1769             return -EINVAL;
1770     }
1771 
1772     return 0;
1773 }
1774 
1775 /**
1776  * hdmi_drm_infoframe_unpack_only() - unpack binary buffer of CTA-861-G DRM
1777  *                                    infoframe DataBytes to a HDMI DRM
1778  *                                    infoframe
1779  * @frame: HDMI DRM infoframe
1780  * @buffer: source buffer
1781  * @size: size of buffer
1782  *
1783  * Unpacks CTA-861-G DRM infoframe DataBytes contained in the binary @buffer
1784  * into a structured @frame of the HDMI Dynamic Range and Mastering (DRM)
1785  * infoframe.
1786  *
1787  * Returns 0 on success or a negative error code on failure.
1788  */
1789 int hdmi_drm_infoframe_unpack_only(struct hdmi_drm_infoframe *frame,
1790                    const void *buffer, size_t size)
1791 {
1792     const u8 *ptr = buffer;
1793     const u8 *temp;
1794     u8 x_lsb, x_msb;
1795     u8 y_lsb, y_msb;
1796     int ret;
1797     int i;
1798 
1799     if (size < HDMI_DRM_INFOFRAME_SIZE)
1800         return -EINVAL;
1801 
1802     ret = hdmi_drm_infoframe_init(frame);
1803     if (ret)
1804         return ret;
1805 
1806     frame->eotf = ptr[0] & 0x7;
1807     frame->metadata_type = ptr[1] & 0x7;
1808 
1809     temp = ptr + 2;
1810     for (i = 0; i < 3; i++) {
1811         x_lsb = *temp++;
1812         x_msb = *temp++;
1813         frame->display_primaries[i].x = (x_msb << 8) | x_lsb;
1814         y_lsb = *temp++;
1815         y_msb = *temp++;
1816         frame->display_primaries[i].y = (y_msb << 8) | y_lsb;
1817     }
1818 
1819     frame->white_point.x = (ptr[15] << 8) | ptr[14];
1820     frame->white_point.y = (ptr[17] << 8) | ptr[16];
1821 
1822     frame->max_display_mastering_luminance = (ptr[19] << 8) | ptr[18];
1823     frame->min_display_mastering_luminance = (ptr[21] << 8) | ptr[20];
1824     frame->max_cll = (ptr[23] << 8) | ptr[22];
1825     frame->max_fall = (ptr[25] << 8) | ptr[24];
1826 
1827     return 0;
1828 }
1829 EXPORT_SYMBOL(hdmi_drm_infoframe_unpack_only);
1830 
1831 /**
1832  * hdmi_drm_infoframe_unpack() - unpack binary buffer to a HDMI DRM infoframe
1833  * @frame: HDMI DRM infoframe
1834  * @buffer: source buffer
1835  * @size: size of buffer
1836  *
1837  * Unpacks the CTA-861-G DRM infoframe contained in the binary @buffer into
1838  * a structured @frame of the HDMI Dynamic Range and Mastering (DRM)
1839  * infoframe. It also verifies the checksum as required by section 5.3.5 of
1840  * the HDMI 1.4 specification.
1841  *
1842  * Returns 0 on success or a negative error code on failure.
1843  */
1844 static int hdmi_drm_infoframe_unpack(struct hdmi_drm_infoframe *frame,
1845                      const void *buffer, size_t size)
1846 {
1847     const u8 *ptr = buffer;
1848     int ret;
1849 
1850     if (size < HDMI_INFOFRAME_SIZE(DRM))
1851         return -EINVAL;
1852 
1853     if (ptr[0] != HDMI_INFOFRAME_TYPE_DRM ||
1854         ptr[1] != 1 ||
1855         ptr[2] != HDMI_DRM_INFOFRAME_SIZE)
1856         return -EINVAL;
1857 
1858     if (hdmi_infoframe_checksum(buffer, HDMI_INFOFRAME_SIZE(DRM)) != 0)
1859         return -EINVAL;
1860 
1861     ret = hdmi_drm_infoframe_unpack_only(frame, ptr + HDMI_INFOFRAME_HEADER_SIZE,
1862                          size - HDMI_INFOFRAME_HEADER_SIZE);
1863     return ret;
1864 }
1865 
1866 /**
1867  * hdmi_infoframe_unpack() - unpack binary buffer to a HDMI infoframe
1868  * @frame: HDMI infoframe
1869  * @buffer: source buffer
1870  * @size: size of buffer
1871  *
1872  * Unpacks the information contained in binary buffer @buffer into a structured
1873  * @frame of a HDMI infoframe.
1874  * Also verifies the checksum as required by section 5.3.5 of the HDMI 1.4
1875  * specification.
1876  *
1877  * Returns 0 on success or a negative error code on failure.
1878  */
1879 int hdmi_infoframe_unpack(union hdmi_infoframe *frame,
1880               const void *buffer, size_t size)
1881 {
1882     int ret;
1883     const u8 *ptr = buffer;
1884 
1885     if (size < HDMI_INFOFRAME_HEADER_SIZE)
1886         return -EINVAL;
1887 
1888     switch (ptr[0]) {
1889     case HDMI_INFOFRAME_TYPE_AVI:
1890         ret = hdmi_avi_infoframe_unpack(&frame->avi, buffer, size);
1891         break;
1892     case HDMI_INFOFRAME_TYPE_DRM:
1893         ret = hdmi_drm_infoframe_unpack(&frame->drm, buffer, size);
1894         break;
1895     case HDMI_INFOFRAME_TYPE_SPD:
1896         ret = hdmi_spd_infoframe_unpack(&frame->spd, buffer, size);
1897         break;
1898     case HDMI_INFOFRAME_TYPE_AUDIO:
1899         ret = hdmi_audio_infoframe_unpack(&frame->audio, buffer, size);
1900         break;
1901     case HDMI_INFOFRAME_TYPE_VENDOR:
1902         ret = hdmi_vendor_any_infoframe_unpack(&frame->vendor, buffer, size);
1903         break;
1904     default:
1905         ret = -EINVAL;
1906         break;
1907     }
1908 
1909     return ret;
1910 }
1911 EXPORT_SYMBOL(hdmi_infoframe_unpack);