Home | History | Annotate | Line # | Download | only in linux
hdmi.h revision 1.1
      1 /*	$NetBSD: hdmi.h,v 1.1 2014/07/16 20:56:25 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Taylor R. Campbell.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #ifndef	_LINUX_HDMI_H_
     33 #define	_LINUX_HDMI_H_
     34 
     35 #include <sys/types.h>
     36 #include <sys/param.h>
     37 #include <sys/errno.h>
     38 #include <sys/systm.h>
     39 
     40 enum hdmi_3d_structure {
     41         HDMI_3D_STRUCTURE_INVALID		= -1,
     42         HDMI_3D_STRUCTURE_FRAME_PACKING		= 0,
     43         HDMI_3D_STRUCTURE_FIELD_ALTERNATIVE	= 1,
     44         HDMI_3D_STRUCTURE_LINE_ALTERNATIVE	= 2,
     45         HDMI_3D_STRUCTURE_SIDE_BY_SIDE_FULL	= 3,
     46         HDMI_3D_STRUCTURE_L_DEPTH		= 4,
     47         HDMI_3D_STRUCTURE_L_DEPTH_GFX_GFX_DEPTH	= 5,
     48         HDMI_3D_STRUCTURE_TOP_AND_BOTTOM	= 6,
     49         HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF	= 8,
     50 };
     51 
     52 enum hdmi_active_aspect {
     53         HDMI_ACTIVE_ASPECT_16_9_TOP		= 2,
     54         HDMI_ACTIVE_ASPECT_14_9_TOP		= 3,
     55         HDMI_ACTIVE_ASPECT_16_9_CENTER		= 4,
     56         HDMI_ACTIVE_ASPECT_PICTURE		= 8,
     57         HDMI_ACTIVE_ASPECT_4_3			= 9,
     58         HDMI_ACTIVE_ASPECT_16_9			= 10,
     59         HDMI_ACTIVE_ASPECT_14_9			= 11,
     60         HDMI_ACTIVE_ASPECT_4_3_SP_14_9		= 13,
     61         HDMI_ACTIVE_ASPECT_16_9_SP_14_9		= 14,
     62         HDMI_ACTIVE_ASPECT_16_9_SP_4_3		= 15,
     63 };
     64 
     65 enum hdmi_colorimetry {
     66 	HDMI_COLORIMETRY_NONE			= 0,
     67 	HDMI_COLORIMETRY_ITU_601		= 1,
     68 	HDMI_COLORIMETRY_ITU_709		= 2,
     69 	HDMI_COLORIMETRY_EXTENDED		= 3,
     70 };
     71 
     72 enum hdmi_colorspace {
     73 	HDMI_COLORSPACE_RGB			= 0,
     74 	HDMI_COLORSPACE_YUV422			= 1,
     75 	HDMI_COLORSPACE_YUV444			= 2,
     76 };
     77 
     78 enum hdmi_content_type {
     79 	HDMI_CONTENT_TYPE_NONE			= 0,
     80 	HDMI_CONTENT_TYPE_PHOTO			= 1,
     81 	HDMI_CONTENT_TYPE_CINEMA		= 2,
     82 	HDMI_CONTENT_TYPE_GAME			= 3,
     83 };
     84 
     85 enum hdmi_extended_colorimetry {
     86 	HDMI_EXTENDED_COLORIMETRY_XV_YCC_601	= 0,
     87 	HDMI_EXTENDED_COLORIMETRY_XV_YCC_709	= 1,
     88 	HDMI_EXTENDED_COLORIMETRY_S_YCC_601	= 2,
     89 	HDMI_EXTENDED_COLORIMETRY_ADOBE_YCC_601	= 3,
     90 	HDMI_EXTENDED_COLORIMETRY_ADOBE_RGB	= 4,
     91 };
     92 
     93 enum hdmi_nups {
     94 	HDMI_NUPS_UNKNOWN			= 0,
     95 	HDMI_NUPS_HORIZONTAL			= 1,
     96 	HDMI_NUPS_VERTICAL			= 2,
     97 	HDMI_NUPS_BOTH				= 3,
     98 };
     99 
    100 enum hdmi_picture_aspect {
    101 	HDMI_PICTURE_ASPECT_NONE		= 0,
    102 	HDMI_PICTURE_ASPECT_4_3			= 1,
    103 	HDMI_PICTURE_ASPECT_16_9		= 2,
    104 };
    105 
    106 enum hdmi_quantization_range {
    107 	HDMI_QUANTIZATION_RANGE_DEFAULT		= 0,
    108 	HDMI_QUANTIZATION_RANGE_LIMITED		= 1,
    109 	HDMI_QUANTIZATION_RANGE_FULL		= 2,
    110 };
    111 
    112 enum hdmi_scan_mode {
    113 	HDMI_SCAN_MODE_NONE			= 0,
    114 	HDMI_SCAN_MODE_OVERSCAN			= 1,
    115 	HDMI_SCAN_MODE_UNDERSCAN		= 2,
    116 };
    117 
    118 enum hdmi_ycc_quantization_range {
    119 	HDMI_YCC_QUANTIZATION_RANGE_LIMITED	= 0,
    120 	HDMI_YCC_QUANTIZATION_RANGE_FULL	= 1,
    121 };
    122 
    123 enum hdmi_infoframe_type {
    124 	HDMI_INFOFRAME_TYPE_VENDOR		= 0x81,
    125 	HDMI_INFOFRAME_TYPE_AVI			= 0x82,
    126 	HDMI_INFOFRAME_TYPE_SPD			= 0x83,
    127 	HDMI_INFOFRAME_TYPE_AUDIO		= 0x84,
    128 };
    129 
    130 #define	HDMI_INFOFRAME_SIZE(TYPE)					      \
    131 	(HDMI_INFOFRAME_HEADER_SIZE + HDMI_##TYPE##_INFOFRAME_SIZE)
    132 
    133 #define	HDMI_INFOFRAME_HEADER_SIZE	4
    134 struct hdmi_infoframe_header {
    135 	enum hdmi_infoframe_type	type;
    136 	uint8_t				version;
    137 	uint8_t				length;
    138 	/* checksum */
    139 };
    140 
    141 static inline void
    142 hdmi_infoframe_header_init(struct hdmi_infoframe_header *header,
    143     enum hdmi_infoframe_type type, uint8_t vers, uint8_t length)
    144 {
    145 
    146 	header->type = type;
    147 	header->version = vers;
    148 	header->length = length;
    149 }
    150 
    151 static inline int
    152 hdmi_infoframe_header_pack(const struct hdmi_infoframe_header *header,
    153     uint8_t length, void *buf, size_t size)
    154 {
    155 	uint8_t *const p = buf;
    156 
    157 	if ((size < length) ||
    158 	    (size - length < HDMI_INFOFRAME_HEADER_SIZE))
    159 		return -ENOSPC;
    160 
    161 	p[0] = header->type;
    162 	p[1] = header->version;
    163 	p[2] = length;
    164 	p[3] = 0;		/* checksum */
    165 
    166 	return HDMI_INFOFRAME_HEADER_SIZE;
    167 }
    168 
    169 static inline void
    170 hdmi_infoframe_checksum(void *buf, size_t length)
    171 {
    172 	uint8_t *p = buf;
    173 	uint8_t checksum = 0;
    174 
    175 	while (length--)
    176 		checksum = *p++;
    177 
    178 	p = buf;
    179 	p[3] = (256 - checksum);
    180 }
    181 
    182 #define	HDMI_AVI_INFOFRAME_SIZE		13
    183 struct hdmi_avi_infoframe {
    184 	struct hdmi_infoframe_header	header;
    185 	enum hdmi_colorspace		colorspace;
    186 	enum hdmi_scan_mode		scan_mode;
    187 	enum hdmi_colorimetry		colorimetry;
    188 	enum hdmi_picture_aspect	picture_aspect;
    189 	enum hdmi_active_aspect		active_aspect;
    190 	bool				itc;
    191 	enum hdmi_extended_colorimetry	extended_colorimetry;
    192 	enum hdmi_quantization_range	quantization_range;
    193 	enum hdmi_nups			nups;
    194 	uint8_t				video_code;
    195 	enum hdmi_ycc_quantization_range ycc_quantization_range;
    196 	enum hdmi_content_type		content_type;
    197 	uint8_t				pixel_repeat;
    198 	uint16_t			top_bar;
    199 	uint16_t			bottom_bar;
    200 	uint16_t			left_bar;
    201 	uint16_t			right_bar;
    202 };
    203 
    204 static inline int
    205 hdmi_avi_infoframe_init(struct hdmi_avi_infoframe *frame)
    206 {
    207 	static const struct hdmi_avi_infoframe zero_frame;
    208 
    209 	*frame = zero_frame;
    210 
    211 	hdmi_infoframe_header_init(&frame->header, HDMI_INFOFRAME_TYPE_AVI, 2,
    212 	    HDMI_AVI_INFOFRAME_SIZE);
    213 
    214 	return 0;
    215 }
    216 
    217 static inline ssize_t
    218 hdmi_avi_infoframe_pack(const struct hdmi_avi_infoframe *frame, void *buf,
    219     size_t size)
    220 {
    221 	const size_t length = HDMI_INFOFRAME_HEADER_SIZE +
    222 	    HDMI_AVI_INFOFRAME_SIZE;
    223 	uint8_t *p = buf;
    224 	int ret;
    225 
    226 	KASSERT(frame->header.length == HDMI_AVI_INFOFRAME_SIZE);
    227 
    228 	ret = hdmi_infoframe_header_pack(&frame->header, length, p, size);
    229 	if (ret < 0)
    230 		return ret;
    231 	p += HDMI_INFOFRAME_HEADER_SIZE;
    232 	size -= HDMI_INFOFRAME_HEADER_SIZE;
    233 
    234 	p[0] = __SHIFTIN(frame->colorspace, __BITS(6,5));
    235 	p[0] |= __SHIFTIN(frame->active_aspect & 0xf? 1 : 0, __BIT(4));
    236 	p[0] |= __SHIFTIN(frame->top_bar || frame->bottom_bar, __BIT(3));
    237 	p[0] |= __SHIFTIN(frame->left_bar || frame->right_bar, __BIT(2));
    238 	p[0] |= __SHIFTIN(frame->scan_mode, __BITS(1,0));
    239 
    240 	p[1] = __SHIFTIN(frame->colorimetry, __BITS(7,6));
    241 	p[1] |= __SHIFTIN(frame->picture_aspect, __BITS(5,4));
    242 	p[1] |= __SHIFTIN(frame->active_aspect, __BITS(3,0));
    243 
    244 	p[2] = __SHIFTIN(frame->itc? 1 : 0, __BIT(7));
    245 	p[2] |= __SHIFTIN(frame->extended_colorimetry, __BITS(6,4));
    246 	p[2] |= __SHIFTIN(frame->quantization_range, __BITS(3,2));
    247 	p[2] |= __SHIFTIN(frame->nups, __BITS(1,0));
    248 
    249 	p[3] = frame->video_code;
    250 
    251 	p[4] = __SHIFTIN(frame->ycc_quantization_range, __BITS(7,6));
    252 	p[4] |= __SHIFTIN(frame->content_type, __BITS(5,4));
    253 	p[4] |= __SHIFTIN(frame->pixel_repeat, __BITS(3,0));
    254 
    255 	le16enc(&p[5], frame->top_bar);
    256 	le16enc(&p[7], frame->bottom_bar);
    257 	le16enc(&p[9], frame->left_bar);
    258 	le16enc(&p[11], frame->right_bar);
    259 	CTASSERT(HDMI_AVI_INFOFRAME_SIZE == 13);
    260 
    261 	hdmi_infoframe_checksum(buf, length);
    262 
    263 	return length;
    264 }
    265 
    266 #define	HDMI_SPD_INFOFRAME_SIZE		25
    267 struct hdmi_spd_infoframe {
    268 	struct hdmi_infoframe_header	header;
    269 	char				vendor[8];
    270 	char				product[16];
    271 	enum hdmi_spd_sdi {
    272 	        HDMI_SPD_SDI_UNKNOWN	= 0,
    273 		HDMI_SPD_SDI_DSTB	= 1,
    274 		HDMI_SPD_SDI_DVDP	= 2,
    275 		HDMI_SPD_SDI_DVHS	= 3,
    276 		HDMI_SPD_SDI_HDDVR	= 4,
    277 		HDMI_SPD_SDI_DVC	= 5,
    278 		HDMI_SPD_SDI_DSC	= 6,
    279 		HDMI_SPD_SDI_VCD	= 7,
    280 		HDMI_SPD_SDI_GAME	= 8,
    281 		HDMI_SPD_SDI_PC		= 9,
    282 		HDMI_SPD_SDI_BD		= 10,
    283 		HDMI_SPD_SDI_SACD	= 11,
    284 		HDMI_SPD_SDI_HDDVD	= 12,
    285 		HDMI_SPD_SDI_PMP	= 13,
    286 	}				sdi;
    287 };
    288 
    289 static inline int
    290 hdmi_spd_infoframe_init(struct hdmi_spd_infoframe *frame, const char *vendor,
    291     const char *product)
    292 {
    293 	static const struct hdmi_spd_infoframe zero_frame;
    294 
    295 	*frame = zero_frame;
    296 
    297 	hdmi_infoframe_header_init(&frame->header, HDMI_INFOFRAME_TYPE_SPD,
    298 	    1, HDMI_SPD_INFOFRAME_SIZE);
    299 
    300 	(void)strlcpy(frame->vendor, vendor, sizeof(frame->vendor));
    301 	(void)strlcpy(frame->product, product, sizeof(frame->product));
    302 
    303 	return 0;
    304 }
    305 
    306 static inline ssize_t
    307 hdmi_spd_infoframe_pack(struct hdmi_spd_infoframe *frame, void *buf,
    308     size_t size)
    309 {
    310 	const size_t length = HDMI_INFOFRAME_HEADER_SIZE +
    311 	    HDMI_SPD_INFOFRAME_SIZE;
    312 	uint8_t *p = buf;
    313 	int ret;
    314 
    315 	KASSERT(frame->header.length == HDMI_SPD_INFOFRAME_SIZE);
    316 
    317 	ret = hdmi_infoframe_header_pack(&frame->header, length, p, size);
    318 	if (ret < 0)
    319 		return ret;
    320 	p += HDMI_INFOFRAME_HEADER_SIZE;
    321 	size -= HDMI_INFOFRAME_HEADER_SIZE;
    322 
    323 	(void)memcpy(&p[0], frame->vendor, 8);
    324 	(void)memcpy(&p[8], frame->product, 16);
    325 	p[24] = frame->sdi;
    326 
    327 	hdmi_infoframe_checksum(buf, length);
    328 
    329 	return length;
    330 }
    331 
    332 #define	HDMI_IEEE_OUI	0x000c03
    333 
    334 struct hdmi_vendor_infoframe {
    335 	struct hdmi_infoframe_header	header;
    336 	uint32_t			oui;
    337 	uint8_t				vic;
    338 	enum hdmi_3d_structure		s3d_struct;
    339 	unsigned			s3d_ext_data;
    340 };
    341 
    342 union hdmi_vendor_any_infoframe {
    343 	struct {
    344 		struct hdmi_infoframe_header	header;
    345 		uint32_t			oui;
    346 	}				any;
    347 	struct hdmi_vendor_infoframe	hdmi;
    348 };
    349 
    350 static inline int
    351 hdmi_vendor_infoframe_init(struct hdmi_vendor_infoframe *frame)
    352 {
    353 	static const struct hdmi_vendor_infoframe zero_frame;
    354 
    355 	*frame = zero_frame;
    356 
    357 	hdmi_infoframe_header_init(&frame->header, HDMI_INFOFRAME_TYPE_VENDOR,
    358 	    1, 0 /* depends on s3d_struct */);
    359 
    360 	frame->oui = HDMI_IEEE_OUI;
    361 	frame->s3d_struct = HDMI_3D_STRUCTURE_INVALID;
    362 
    363 	return 0;
    364 }
    365 
    366 static inline int
    367 hdmi_vendor_infoframe_pack(const struct hdmi_vendor_infoframe *frame,
    368     void *buf, size_t size)
    369 {
    370 	uint8_t *p = buf;
    371 	size_t length;
    372 	int ret;
    373 
    374 	/* Exactly one must be supplied.  */
    375 	if ((frame->vic == 0) ==
    376 	    (frame->s3d_struct == HDMI_3D_STRUCTURE_INVALID))
    377 		return -EINVAL;
    378 
    379 	length = HDMI_INFOFRAME_HEADER_SIZE + 6;
    380 	if (frame->s3d_struct >= HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF)
    381 		length += 1;
    382 
    383 	ret = hdmi_infoframe_header_pack(&frame->header, length, p, size);
    384 	if (ret < 0)
    385 		return ret;
    386 	p += HDMI_INFOFRAME_HEADER_SIZE;
    387 	size -= HDMI_INFOFRAME_HEADER_SIZE;
    388 
    389 	p[0] = 0x03;
    390 	p[1] = 0x0c;
    391 	p[2] = 0x00;
    392 
    393 	if (frame->vic == 0) {
    394 		p[3] = __SHIFTIN(0x2, __BITS(6,5));
    395 		p[4] = __SHIFTIN(frame->s3d_struct, __BITS(7,4));
    396 		if (frame->s3d_struct >= HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF)
    397 			p[9] = __SHIFTIN(frame->s3d_ext_data, __BITS(7,4));
    398 	} else {
    399 		p[3] = __SHIFTIN(0x1, __BITS(6,5));
    400 		p[4] = frame->vic;
    401 	}
    402 
    403 	hdmi_infoframe_checksum(buf, length);
    404 
    405 	return length;
    406 }
    407 
    408 union hdmi_infoframe {
    409 	struct hdmi_infoframe_header	any;
    410 	struct hdmi_avi_infoframe	avi;
    411 	struct hdmi_spd_infoframe	spd;
    412 	union hdmi_vendor_any_infoframe	vendor;
    413 };
    414 
    415 static inline ssize_t
    416 hdmi_infoframe_pack(union hdmi_infoframe *frame, void *buf, size_t size)
    417 {
    418 
    419 	switch (frame->any.type) {
    420 	case HDMI_INFOFRAME_TYPE_AVI:
    421 		return hdmi_avi_infoframe_pack(&frame->avi, buf, size);
    422 	case HDMI_INFOFRAME_TYPE_SPD:
    423 		return hdmi_spd_infoframe_pack(&frame->spd, buf, size);
    424 	case HDMI_INFOFRAME_TYPE_VENDOR:
    425 		return hdmi_vendor_infoframe_pack(&frame->vendor.hdmi, buf,
    426 		    size);
    427 	default:
    428 		return -EINVAL;
    429 	}
    430 }
    431 
    432 #endif	/* _LINUX_HDMI_H_ */
    433