Linux Audio

Check our new training course

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