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