hdafg_dd.c revision 1.2 1 1.2 mrg /* $NetBSD: hdafg_dd.c,v 1.2 2017/08/04 00:25:23 mrg Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*
4 1.1 jmcneill * Copyright (c) 2011 Jared D. McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. The name of the author may not be used to endorse or promote products
13 1.1 jmcneill * derived from this software without specific prior written permission.
14 1.1 jmcneill *
15 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1 jmcneill * SUCH DAMAGE.
26 1.1 jmcneill */
27 1.1 jmcneill
28 1.1 jmcneill /*
29 1.1 jmcneill * HD audio Digital Display support
30 1.1 jmcneill */
31 1.1 jmcneill
32 1.1 jmcneill #include <sys/cdefs.h>
33 1.2 mrg __KERNEL_RCSID(0, "$NetBSD: hdafg_dd.c,v 1.2 2017/08/04 00:25:23 mrg Exp $");
34 1.1 jmcneill
35 1.1 jmcneill #include <sys/types.h>
36 1.1 jmcneill #include <sys/param.h>
37 1.1 jmcneill #include <sys/systm.h>
38 1.1 jmcneill #include <sys/kernel.h>
39 1.1 jmcneill #include <sys/device.h>
40 1.1 jmcneill #include <sys/conf.h>
41 1.1 jmcneill #include <sys/bus.h>
42 1.1 jmcneill
43 1.1 jmcneill #include "hdaudioreg.h"
44 1.1 jmcneill #include "hdaudiovar.h"
45 1.1 jmcneill #include "hdafg_dd.h"
46 1.1 jmcneill
47 1.1 jmcneill int
48 1.1 jmcneill hdafg_dd_parse_info(uint8_t *data, size_t datalen, struct hdafg_dd_info *hdi)
49 1.1 jmcneill {
50 1.1 jmcneill struct eld_baseline_block *block = &hdi->eld;
51 1.1 jmcneill unsigned int i;
52 1.1 jmcneill
53 1.2 mrg #ifdef HDAFG_HDMI_DEBUG
54 1.1 jmcneill printf("hdafg_dd_parse_info: datalen=%u\n", (unsigned int)datalen);
55 1.2 mrg #endif
56 1.1 jmcneill
57 1.1 jmcneill memset(hdi, 0, sizeof(*hdi));
58 1.1 jmcneill
59 1.1 jmcneill if (datalen < sizeof(block->header)) {
60 1.2 mrg #ifdef HDAFG_HDMI_DEBUG
61 1.1 jmcneill printf(" no room for header\n");
62 1.2 mrg #endif
63 1.1 jmcneill return EINVAL;
64 1.1 jmcneill }
65 1.1 jmcneill
66 1.1 jmcneill memcpy(&block->header, data, sizeof(block->header));
67 1.1 jmcneill data += sizeof(block->header);
68 1.1 jmcneill datalen -= sizeof(block->header);
69 1.1 jmcneill
70 1.1 jmcneill if (datalen < block->header.baseline_eld_len * 4 ||
71 1.1 jmcneill datalen < sizeof(*block) - sizeof(block->header)) {
72 1.2 mrg #ifdef HDAFG_HDMI_DEBUG
73 1.1 jmcneill printf(" ack!\n");
74 1.2 mrg #endif
75 1.1 jmcneill return EINVAL;
76 1.1 jmcneill }
77 1.1 jmcneill
78 1.1 jmcneill datalen = block->header.baseline_eld_len * 4;
79 1.1 jmcneill
80 1.1 jmcneill memcpy(&block->flags[0], data, sizeof(*block) - sizeof(block->header));
81 1.1 jmcneill data += sizeof(*block) - sizeof(block->header);
82 1.1 jmcneill datalen -= sizeof(*block) - sizeof(block->header);
83 1.1 jmcneill
84 1.1 jmcneill if (datalen < ELD_MNL(block)) {
85 1.2 mrg #ifdef HDAFG_HDMI_DEBUG
86 1.1 jmcneill printf(" MNL=%u\n", ELD_MNL(block));
87 1.2 mrg #endif
88 1.1 jmcneill return EINVAL;
89 1.1 jmcneill }
90 1.1 jmcneill
91 1.1 jmcneill memcpy(hdi->monitor, data, ELD_MNL(block));
92 1.1 jmcneill data += ELD_MNL(block);
93 1.1 jmcneill datalen -= ELD_MNL(block);
94 1.1 jmcneill
95 1.1 jmcneill if (datalen != ELD_SAD_COUNT(block) * sizeof(hdi->sad[0])) {
96 1.2 mrg #ifdef HDAFG_HDMI_DEBUG
97 1.1 jmcneill printf(" datalen %u sadcount %u sizeof sad %u\n",
98 1.1 jmcneill (unsigned int)datalen,
99 1.1 jmcneill ELD_SAD_COUNT(block),
100 1.1 jmcneill (unsigned int)sizeof(hdi->sad[0]));
101 1.2 mrg #endif
102 1.1 jmcneill return EINVAL;
103 1.1 jmcneill }
104 1.1 jmcneill hdi->nsad = ELD_SAD_COUNT(block);
105 1.1 jmcneill for (i = 0; i < hdi->nsad; i++) {
106 1.1 jmcneill memcpy(&hdi->sad[i], data, sizeof(hdi->sad[i]));
107 1.1 jmcneill data += sizeof(hdi->sad[i]);
108 1.1 jmcneill datalen -= sizeof(hdi->sad[i]);
109 1.1 jmcneill }
110 1.1 jmcneill
111 1.2 mrg #ifdef HDAFG_HDMI_DEBUG
112 1.1 jmcneill printf("datalen = %u\n", (unsigned int)datalen);
113 1.2 mrg #endif
114 1.1 jmcneill KASSERT(datalen == 0);
115 1.1 jmcneill
116 1.1 jmcneill return 0;
117 1.1 jmcneill }
118 1.1 jmcneill
119 1.1 jmcneill void
120 1.1 jmcneill hdafg_dd_hdmi_ai_cksum(struct hdmi_audio_infoframe *hdmi)
121 1.1 jmcneill {
122 1.1 jmcneill uint8_t *dip = (uint8_t *)hdmi, c = 0;
123 1.1 jmcneill int i;
124 1.1 jmcneill
125 1.1 jmcneill hdmi->checksum = 0;
126 1.1 jmcneill for (i = 0; i < sizeof(*hdmi); i++)
127 1.1 jmcneill c += dip[i];
128 1.1 jmcneill hdmi->checksum = -c;
129 1.1 jmcneill }
130