Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 24 Jun 2019 19:31:32 +0000 (UTC)
From:      Scott Long <scottl@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r349335 - head/usr.sbin/bhyve
Message-ID:  <201906241931.x5OJVWVs025283@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: scottl
Date: Mon Jun 24 19:31:32 2019
New Revision: 349335
URL: https://svnweb.freebsd.org/changeset/base/349335

Log:
  Add the PCI HDAudio device model from the 2016 GSoC.  Detailed information
  can be found at
  
  https://wiki.freebsd.org/SummerOfCode2016/HDAudioEmulationForBhyve
  
  This commit has evolved from the original work to include Capsicum
  integration.  As part of that, it only opens the host audio devices once
  and leaves them open, instead of opening and closing them on each guest
  access.  Thanks to Peter Grehan and Marcelo Araujo for their help in
  bringing the work forward and providing some of the final techncial push.
  
  Submitted by:	Alex Teaca <iateaca@freebsd.org>
  Differential Revision:	D7840, D12419

Added:
  head/usr.sbin/bhyve/audio.c   (contents, props changed)
  head/usr.sbin/bhyve/audio.h   (contents, props changed)
  head/usr.sbin/bhyve/hda_codec.c   (contents, props changed)
  head/usr.sbin/bhyve/hda_reg.h   (contents, props changed)
  head/usr.sbin/bhyve/hdac_reg.h   (contents, props changed)
  head/usr.sbin/bhyve/pci_hda.c   (contents, props changed)
  head/usr.sbin/bhyve/pci_hda.h   (contents, props changed)
Modified:
  head/usr.sbin/bhyve/Makefile

Modified: head/usr.sbin/bhyve/Makefile
==============================================================================
--- head/usr.sbin/bhyve/Makefile	Mon Jun 24 19:19:37 2019	(r349334)
+++ head/usr.sbin/bhyve/Makefile	Mon Jun 24 19:31:32 2019	(r349335)
@@ -16,6 +16,7 @@ BHYVE_SYSDIR?=${SRCTOP}
 SRCS=	\
 	atkbdc.c		\
 	acpi.c			\
+	audio.c			\
 	bhyvegc.c		\
 	bhyverun.c		\
 	block_if.c		\
@@ -27,6 +28,7 @@ SRCS=	\
 	dbgport.c		\
 	fwctl.c			\
 	gdb.c			\
+	hda_codec.c		\
 	inout.c			\
 	ioapic.c		\
 	mem.c			\
@@ -36,6 +38,7 @@ SRCS=	\
 	pci_ahci.c		\
 	pci_e82545.c		\
 	pci_emul.c		\
+	pci_hda.c		\
 	pci_fbuf.c		\
 	pci_hostbridge.c	\
 	pci_irq.c		\

Added: head/usr.sbin/bhyve/audio.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/usr.sbin/bhyve/audio.c	Mon Jun 24 19:31:32 2019	(r349335)
@@ -0,0 +1,282 @@
+/*-
+ * Copyright (c) 2016 Alex Teaca <iateaca@FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#ifndef WITHOUT_CAPSICUM
+#include <sys/capsicum.h>
+#include <capsicum_helpers.h>
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <unistd.h>
+#include <assert.h>
+#include <errno.h>
+#include <err.h>
+#include <sysexits.h>
+
+#include "audio.h"
+#include "pci_hda.h"
+
+/*
+ * Audio Player internal data structures
+ */
+
+struct audio {
+	int fd;
+	uint8_t dir;
+	uint8_t inited;
+	char dev_name[64];
+};
+
+/*
+ * Audio Player module function definitions
+ */
+
+/*
+ * audio_init - initialize an instance of audio player
+ * @dev_name - the backend sound device used to play / capture
+ * @dir - dir = 1 for write mode, dir = 0 for read mode
+ */
+struct audio *
+audio_init(const char *dev_name, uint8_t dir)
+{
+	struct audio *aud = NULL;
+#ifndef WITHOUT_CAPSICUM
+	cap_rights_t rights;
+	cap_ioctl_t cmds[] = {
+	    SNDCTL_DSP_RESET, SNDCTL_DSP_SETFMT, SNDCTL_DSP_CHANNELS,
+	    SNDCTL_DSP_SPEED,
+#ifdef DEBUG_HDA
+	    SNDCTL_DSP_GETOSPACE, SNDCTL_DSP_GETISPACE,
+#endif
+	};
+#endif
+
+	assert(dev_name);
+
+	aud = calloc(1, sizeof(*aud));
+	if (!aud)
+		return NULL;
+
+	if (strlen(dev_name) < sizeof(aud->dev_name))
+		memcpy(aud->dev_name, dev_name, strlen(dev_name) + 1);
+	else {
+		DPRINTF("dev_name too big\n");
+		free(aud);
+		return NULL;
+	}
+
+	aud->dir = dir;
+
+	aud->fd = open(aud->dev_name, aud->dir ? O_WRONLY : O_RDONLY, 0);
+	if (aud->fd == -1) {
+		DPRINTF("Failed to open dev: %s, errno: %d\n",
+		    aud->dev_name, errno);
+		return (NULL);
+	}
+
+#ifndef WITHOUT_CAPSICUM
+	cap_rights_init(&rights, CAP_IOCTL, CAP_READ, CAP_WRITE);
+	if (caph_rights_limit(aud->fd, &rights) == -1)
+		errx(EX_OSERR, "Unable to apply rights for sandbox");
+	if (caph_ioctls_limit(aud->fd, cmds, nitems(cmds)) == -1)
+		errx(EX_OSERR, "Unable to limit ioctl rights for sandbox");
+#endif
+
+	return aud;
+}
+
+/*
+ * audio_set_params - reset the sound device and set the audio params
+ * @aud - the audio player to be configured
+ * @params - the audio parameters to be set
+ */
+int
+audio_set_params(struct audio *aud, struct audio_params *params)
+{
+	int audio_fd;
+	int format, channels, rate;
+	int err;
+#if DEBUG_HDA == 1
+	audio_buf_info info;
+#endif
+
+	assert(aud);
+	assert(params);
+
+	if ((audio_fd = aud->fd) < 0) {
+		DPRINTF("Incorrect audio device descriptor for %s\n",
+		    aud->dev_name);
+		return (-1);
+	}
+
+	/* Reset the device if it was previously opened */
+	if (aud->inited) {
+		err = ioctl(audio_fd, SNDCTL_DSP_RESET, NULL);
+		if (err == -1) {
+			DPRINTF("Failed to reset fd: %d, errno: %d\n",
+			    aud->fd, errno);
+			return (-1);
+		}
+	} else
+		aud->inited = 1;
+
+	/* Set the Format (Bits per Sample) */
+	format = params->format;
+	err = ioctl(audio_fd, SNDCTL_DSP_SETFMT, &format);
+	if (err == -1) {
+		DPRINTF("Fail to set fmt: 0x%x errno: %d\n",
+		    params->format, errno);
+		return -1;
+	}
+
+	/* The device does not support the requested audio format */
+	if (format != params->format) {
+		DPRINTF("Mismatch format: 0x%x params->format: 0x%x\n",
+		    format, params->format);
+		return -1;
+	}
+
+	/* Set the Number of Channels */
+	channels = params->channels;
+	err = ioctl(audio_fd, SNDCTL_DSP_CHANNELS, &channels);
+	if (err == -1) {
+		DPRINTF("Fail to set channels: %d errno: %d\n",
+		    params->channels, errno);
+		return -1;
+	}
+
+	/* The device does not support the requested no. of channels */
+	if (channels != params->channels) {
+		DPRINTF("Mismatch channels: %d params->channels: %d\n",
+		    channels, params->channels);
+		return -1;
+	}
+
+	/* Set the Sample Rate / Speed */
+	rate = params->rate;
+	err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &rate);
+	if (err == -1) {
+		DPRINTF("Fail to set speed: %d errno: %d\n",
+		    params->rate, errno);
+		return -1;
+	}
+
+	/* The device does not support the requested rate / speed */
+	if (rate != params->rate) {
+		DPRINTF("Mismatch rate: %d params->rate: %d\n",
+		    rate, params->rate);
+		return -1;
+	}
+
+#if DEBUG_HDA == 1
+	err = ioctl(audio_fd, aud->dir ? SNDCTL_DSP_GETOSPACE :
+	    SNDCTL_DSP_GETISPACE, &info);
+	if (err == -1) {
+		DPRINTF("Fail to get audio buf info errno: %d\n", errno);
+		return -1;
+	}
+	DPRINTF("fragstotal: 0x%x fragsize: 0x%x\n",
+	    info.fragstotal, info.fragsize);
+#endif
+	return 0;
+}
+
+/*
+ * audio_playback - plays samples to the sound device using blocking operations
+ * @aud - the audio player used to play the samples
+ * @buf - the buffer containing the samples
+ * @count - the number of bytes in buffer
+ */
+int
+audio_playback(struct audio *aud, const void *buf, size_t count)
+{
+	int audio_fd = -1;
+	ssize_t len = 0, total = 0;
+
+	assert(aud);
+	assert(aud->dir);
+	assert(buf);
+
+	audio_fd = aud->fd;
+	assert(audio_fd != -1);
+
+	total = 0;
+	while (total < count) {
+		len = write(audio_fd, buf + total, count - total);
+		if (len == -1) {
+			DPRINTF("Fail to write to fd: %d, errno: %d\n",
+			    audio_fd, errno);
+			return -1;
+		}
+
+		total += len;
+	}
+
+	return 0;
+}
+
+/*
+ * audio_record - records samples from the sound device using
+ * blocking operations.
+ * @aud - the audio player used to capture the samples
+ * @buf - the buffer to receive the samples
+ * @count - the number of bytes to capture in buffer
+ * Returns -1 on error and 0 on success
+ */
+int
+audio_record(struct audio *aud, void *buf, size_t count)
+{
+	int audio_fd = -1;
+	ssize_t len = 0, total = 0;
+
+	assert(aud);
+	assert(!aud->dir);
+	assert(buf);
+
+	audio_fd = aud->fd;
+	assert(audio_fd != -1);
+
+	total = 0;
+	while (total < count) {
+		len = read(audio_fd, buf + total, count - total);
+		if (len == -1) {
+			DPRINTF("Fail to write to fd: %d, errno: %d\n",
+			    audio_fd, errno);
+			return -1;
+		}
+
+		total += len;
+	}
+
+	return 0;
+}

Added: head/usr.sbin/bhyve/audio.h
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/usr.sbin/bhyve/audio.h	Mon Jun 24 19:31:32 2019	(r349335)
@@ -0,0 +1,86 @@
+/*-
+ * Copyright (c) 2016 Alex Teaca <iateaca@FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef _AUDIO_EMUL_H_ 
+#define _AUDIO_EMUL_H_
+
+#include <sys/types.h>
+#include <sys/soundcard.h>
+
+/*
+ * Audio Player data structures
+ */
+
+struct audio;
+
+struct audio_params {
+	int channels;
+	int format;
+	int rate;
+};
+
+/*
+ * Audio Player API
+ */
+
+/*
+ * audio_init - initialize an instance of audio player
+ * @dev_name - the backend sound device used to play / capture
+ * @dir - dir = 1 for write mode, dir = 0 for read mode
+ * Returns NULL on error and the address of the audio player instance
+ */
+struct audio *audio_init(const char *dev_name, uint8_t dir);
+
+/*
+ * audio_set_params - reset the sound device and set the audio params
+ * @aud - the audio player to be configured
+ * @params - the audio parameters to be set
+ * Returns -1 on error and 0 on success
+ */
+int audio_set_params(struct audio *aud, struct audio_params *params);
+
+/*
+ * audio_playback - plays samples to the sound device using blocking operations
+ * @aud - the audio player used to play the samples
+ * @buf - the buffer containing the samples
+ * @count - the number of bytes in buffer
+ * Returns -1 on error and 0 on success
+ */
+int audio_playback(struct audio *aud, const void *buf, size_t count);
+
+/*
+ * audio_record - records samples from the sound device using blocking
+ * operations.
+ * @aud - the audio player used to capture the samples
+ * @buf - the buffer to receive the samples
+ * @count - the number of bytes to capture in buffer
+ * Returns -1 on error and 0 on success
+ */
+int audio_record(struct audio *aud, void *buf, size_t count);
+
+#endif  /* _AUDIO_EMUL_H_ */

Added: head/usr.sbin/bhyve/hda_codec.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/usr.sbin/bhyve/hda_codec.c	Mon Jun 24 19:31:32 2019	(r349335)
@@ -0,0 +1,950 @@
+/*-
+ * Copyright (c) 2016 Alex Teaca <iateaca@FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <pthread.h>
+#include <pthread_np.h>
+#include <unistd.h>
+
+#include "pci_hda.h"
+#include "audio.h"
+
+/*
+ * HDA Codec defines
+ */
+#define INTEL_VENDORID				0x8086
+
+#define HDA_CODEC_SUBSYSTEM_ID			((INTEL_VENDORID << 16) | 0x01)
+#define HDA_CODEC_ROOT_NID			0x00
+#define HDA_CODEC_FG_NID			0x01
+#define HDA_CODEC_AUDIO_OUTPUT_NID		0x02
+#define HDA_CODEC_PIN_OUTPUT_NID		0x03
+#define HDA_CODEC_AUDIO_INPUT_NID		0x04
+#define HDA_CODEC_PIN_INPUT_NID			0x05
+
+#define HDA_CODEC_STREAMS_COUNT			0x02
+#define HDA_CODEC_STREAM_OUTPUT			0x00
+#define HDA_CODEC_STREAM_INPUT			0x01
+
+#define HDA_CODEC_PARAMS_COUNT			0x14
+#define HDA_CODEC_CONN_LIST_COUNT		0x01
+#define HDA_CODEC_RESPONSE_EX_UNSOL		0x10
+#define HDA_CODEC_RESPONSE_EX_SOL		0x00
+#define HDA_CODEC_AMP_NUMSTEPS			0x4a
+
+#define HDA_CODEC_SUPP_STREAM_FORMATS_PCM				\
+	(1 << HDA_PARAM_SUPP_STREAM_FORMATS_PCM_SHIFT)
+
+#define HDA_CODEC_FMT_BASE_MASK			(0x01 << 14)
+
+#define HDA_CODEC_FMT_MULT_MASK			(0x07 << 11)
+#define HDA_CODEC_FMT_MULT_2			(0x01 << 11)
+#define HDA_CODEC_FMT_MULT_3			(0x02 << 11)
+#define HDA_CODEC_FMT_MULT_4			(0x03 << 11)
+
+#define HDA_CODEC_FMT_DIV_MASK			0x07
+#define HDA_CODEC_FMT_DIV_SHIFT			8
+
+#define HDA_CODEC_FMT_BITS_MASK			(0x07 << 4)
+#define HDA_CODEC_FMT_BITS_8			(0x00 << 4)
+#define HDA_CODEC_FMT_BITS_16			(0x01 << 4)
+#define HDA_CODEC_FMT_BITS_24			(0x03 << 4)
+#define HDA_CODEC_FMT_BITS_32			(0x04 << 4)
+
+#define HDA_CODEC_FMT_CHAN_MASK			(0x0f << 0)
+
+#define HDA_CODEC_AUDIO_WCAP_OUTPUT					\
+	(0x00 << HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_SHIFT)
+#define HDA_CODEC_AUDIO_WCAP_INPUT					\
+	(0x01 << HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_SHIFT)
+#define HDA_CODEC_AUDIO_WCAP_PIN					\
+	(0x04 << HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_SHIFT)
+#define HDA_CODEC_AUDIO_WCAP_CONN_LIST					\
+	(1 << HDA_PARAM_AUDIO_WIDGET_CAP_CONN_LIST_SHIFT)
+#define HDA_CODEC_AUDIO_WCAP_FORMAT_OVR					\
+	(1 << HDA_PARAM_AUDIO_WIDGET_CAP_FORMAT_OVR_SHIFT)
+#define HDA_CODEC_AUDIO_WCAP_AMP_OVR					\
+	(1 << HDA_PARAM_AUDIO_WIDGET_CAP_AMP_OVR_SHIFT)
+#define HDA_CODEC_AUDIO_WCAP_OUT_AMP					\
+	(1 << HDA_PARAM_AUDIO_WIDGET_CAP_OUT_AMP_SHIFT)
+#define HDA_CODEC_AUDIO_WCAP_IN_AMP					\
+	(1 << HDA_PARAM_AUDIO_WIDGET_CAP_IN_AMP_SHIFT)
+#define HDA_CODEC_AUDIO_WCAP_STEREO					\
+	(1 << HDA_PARAM_AUDIO_WIDGET_CAP_STEREO_SHIFT)
+
+#define HDA_CODEC_PIN_CAP_OUTPUT					\
+	(1 << HDA_PARAM_PIN_CAP_OUTPUT_CAP_SHIFT)
+#define HDA_CODEC_PIN_CAP_INPUT						\
+	(1 << HDA_PARAM_PIN_CAP_INPUT_CAP_SHIFT)
+#define HDA_CODEC_PIN_CAP_PRESENCE_DETECT				\
+	(1 << HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP_SHIFT)
+
+#define HDA_CODEC_OUTPUT_AMP_CAP_MUTE_CAP				\
+	(1 << HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP_SHIFT)
+#define HDA_CODEC_OUTPUT_AMP_CAP_STEPSIZE				\
+	(0x03 << HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE_SHIFT)
+#define HDA_CODEC_OUTPUT_AMP_CAP_NUMSTEPS				\
+	(HDA_CODEC_AMP_NUMSTEPS << HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS_SHIFT)
+#define HDA_CODEC_OUTPUT_AMP_CAP_OFFSET					\
+	(HDA_CODEC_AMP_NUMSTEPS << HDA_PARAM_OUTPUT_AMP_CAP_OFFSET_SHIFT)
+
+#define HDA_CODEC_SET_AMP_GAIN_MUTE_MUTE	0x80
+#define HDA_CODEC_SET_AMP_GAIN_MUTE_GAIN_MASK	0x7f
+
+#define HDA_CODEC_PIN_SENSE_PRESENCE_PLUGGED	(1 << 31)
+#define HDA_CODEC_PIN_WIDGET_CTRL_OUT_ENABLE				\
+	(1 << HDA_CMD_GET_PIN_WIDGET_CTRL_OUT_ENABLE_SHIFT)
+#define HDA_CODEC_PIN_WIDGET_CTRL_IN_ENABLE				\
+	(1 << HDA_CMD_GET_PIN_WIDGET_CTRL_IN_ENABLE_SHIFT)
+
+#define HDA_CONFIG_DEFAULTCONF_COLOR_BLACK				\
+	(0x01 << HDA_CONFIG_DEFAULTCONF_COLOR_SHIFT)
+#define HDA_CONFIG_DEFAULTCONF_COLOR_RED				\
+	(0x05 << HDA_CONFIG_DEFAULTCONF_COLOR_SHIFT)
+
+#define HDA_CODEC_BUF_SIZE			HDA_FIFO_SIZE
+
+#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
+
+
+/*
+ * HDA Audio Context data structures
+ */
+
+typedef void (*transfer_func_t)(void *arg);
+typedef int (*setup_func_t)(void *arg);
+
+struct hda_audio_ctxt {
+	char name[64];
+	uint8_t run;
+	uint8_t started;
+	void *priv;
+	pthread_t tid;
+	pthread_mutex_t mtx;
+	pthread_cond_t cond;
+	setup_func_t do_setup;
+	transfer_func_t do_transfer;
+};
+
+/*
+ * HDA Audio Context module function declarations
+ */
+
+static void *hda_audio_ctxt_thr(void *arg);
+static int hda_audio_ctxt_init(struct hda_audio_ctxt *actx, const char *tname,
+    transfer_func_t do_transfer, setup_func_t do_setup, void *priv);
+static int hda_audio_ctxt_start(struct hda_audio_ctxt *actx);
+static int hda_audio_ctxt_stop(struct hda_audio_ctxt *actx);
+
+/*
+ * HDA Codec data structures
+ */
+
+struct hda_codec_softc;
+
+typedef uint32_t (*verb_func_t)(struct hda_codec_softc *sc, uint16_t verb,
+				    uint16_t payload);
+
+struct hda_codec_stream {
+	uint8_t buf[HDA_CODEC_BUF_SIZE];
+	uint8_t channel;
+	uint16_t fmt;
+	uint8_t stream;
+
+	uint8_t left_gain;
+	uint8_t right_gain;
+	uint8_t left_mute;
+	uint8_t right_mute;
+
+	struct audio *aud;
+	struct hda_audio_ctxt actx;
+};
+
+struct hda_codec_softc {
+	uint32_t no_nodes;
+	uint32_t subsystem_id;
+	const uint32_t (*get_parameters)[HDA_CODEC_PARAMS_COUNT];
+	const uint8_t (*conn_list)[HDA_CODEC_CONN_LIST_COUNT];
+	const uint32_t *conf_default;
+	const uint8_t *pin_ctrl_default;
+	const verb_func_t *verb_handlers;
+
+	struct hda_codec_inst *hci;
+	struct hda_codec_stream streams[HDA_CODEC_STREAMS_COUNT];
+};
+
+/*
+ * HDA Codec module function declarations
+ */
+static int hda_codec_init(struct hda_codec_inst *hci, const char *play,
+    const char *rec, const char *opts);
+static int hda_codec_reset(struct hda_codec_inst *hci);
+static int hda_codec_command(struct hda_codec_inst *hci, uint32_t cmd_data);
+static int hda_codec_notify(struct hda_codec_inst *hci, uint8_t run,
+    uint8_t stream, uint8_t dir);
+
+static int hda_codec_parse_format(uint16_t fmt, struct audio_params *params);
+
+static uint32_t hda_codec_audio_output_nid(struct hda_codec_softc *sc,
+    uint16_t verb, uint16_t payload);
+static void hda_codec_audio_output_do_transfer(void *arg);
+static int hda_codec_audio_output_do_setup(void *arg);
+static uint32_t hda_codec_audio_input_nid(struct hda_codec_softc *sc,
+    uint16_t verb, uint16_t payload);
+static void hda_codec_audio_input_do_transfer(void *arg);
+static int hda_codec_audio_input_do_setup(void *arg);
+
+static uint32_t hda_codec_audio_inout_nid(struct hda_codec_stream *st,
+    uint16_t verb, uint16_t payload);
+
+/*
+ * HDA Codec global data
+ */
+
+#define HDA_CODEC_ROOT_DESC						\
+	[HDA_CODEC_ROOT_NID] = {					\
+		[HDA_PARAM_VENDOR_ID] = INTEL_VENDORID,			\
+		[HDA_PARAM_REVISION_ID] = 0xffff,			\
+		/* 1 Subnode, StartNid = 1 */				\
+		[HDA_PARAM_SUB_NODE_COUNT] = 0x00010001,		\
+	},								\
+
+#define HDA_CODEC_FG_COMMON_DESC					\
+	[HDA_PARAM_FCT_GRP_TYPE] = HDA_PARAM_FCT_GRP_TYPE_NODE_TYPE_AUDIO,\
+	/* B8 - B32, 8.0 - 192.0kHz */					\
+	[HDA_PARAM_SUPP_PCM_SIZE_RATE] = (0x1f << 16) | 0x7ff,		\
+	[HDA_PARAM_SUPP_STREAM_FORMATS] = HDA_CODEC_SUPP_STREAM_FORMATS_PCM,\
+	[HDA_PARAM_INPUT_AMP_CAP] = 0x00,	/* None */		\
+	[HDA_PARAM_OUTPUT_AMP_CAP] = 0x00,	/* None */		\
+	[HDA_PARAM_GPIO_COUNT] = 0x00,					\
+
+#define HDA_CODEC_FG_OUTPUT_DESC					\
+	[HDA_CODEC_FG_NID] = {						\
+		/* 2 Subnodes, StartNid = 2 */				\
+		[HDA_PARAM_SUB_NODE_COUNT] = 0x00020002,		\
+		HDA_CODEC_FG_COMMON_DESC				\
+	},								\
+
+#define HDA_CODEC_FG_INPUT_DESC						\
+	[HDA_CODEC_FG_NID] = {						\
+		/* 2 Subnodes, StartNid = 4 */				\
+		[HDA_PARAM_SUB_NODE_COUNT] = 0x00040002,		\
+		HDA_CODEC_FG_COMMON_DESC				\
+	},								\
+
+#define HDA_CODEC_FG_DUPLEX_DESC					\
+	[HDA_CODEC_FG_NID] = {						\
+		/* 4 Subnodes, StartNid = 2 */				\
+		[HDA_PARAM_SUB_NODE_COUNT] = 0x00020004,		\
+		HDA_CODEC_FG_COMMON_DESC				\
+	},								\
+
+#define HDA_CODEC_OUTPUT_DESC						\
+	[HDA_CODEC_AUDIO_OUTPUT_NID] = {				\
+		[HDA_PARAM_AUDIO_WIDGET_CAP] = 				\
+				HDA_CODEC_AUDIO_WCAP_OUTPUT |		\
+				HDA_CODEC_AUDIO_WCAP_FORMAT_OVR |	\
+				HDA_CODEC_AUDIO_WCAP_AMP_OVR |		\
+				HDA_CODEC_AUDIO_WCAP_OUT_AMP |		\
+				HDA_CODEC_AUDIO_WCAP_STEREO,		\
+		/* B16, 16.0 - 192.0kHz */				\
+		[HDA_PARAM_SUPP_PCM_SIZE_RATE] = (0x02 << 16) | 0x7fc,	\
+		[HDA_PARAM_SUPP_STREAM_FORMATS] =			\
+				HDA_CODEC_SUPP_STREAM_FORMATS_PCM,	\
+		[HDA_PARAM_INPUT_AMP_CAP] = 0x00,	/* None */	\
+		[HDA_PARAM_CONN_LIST_LENGTH] = 0x00,			\
+		[HDA_PARAM_OUTPUT_AMP_CAP] =				\
+				HDA_CODEC_OUTPUT_AMP_CAP_MUTE_CAP |	\
+				HDA_CODEC_OUTPUT_AMP_CAP_STEPSIZE |	\
+				HDA_CODEC_OUTPUT_AMP_CAP_NUMSTEPS |	\
+				HDA_CODEC_OUTPUT_AMP_CAP_OFFSET,	\
+	},								\
+	[HDA_CODEC_PIN_OUTPUT_NID] = {					\
+		[HDA_PARAM_AUDIO_WIDGET_CAP] =				\
+				HDA_CODEC_AUDIO_WCAP_PIN |		\
+				HDA_CODEC_AUDIO_WCAP_CONN_LIST |	\
+				HDA_CODEC_AUDIO_WCAP_STEREO,		\
+		[HDA_PARAM_PIN_CAP] = HDA_CODEC_PIN_CAP_OUTPUT |	\
+				      HDA_CODEC_PIN_CAP_PRESENCE_DETECT,\
+		[HDA_PARAM_INPUT_AMP_CAP] = 0x00,	/* None */	\
+		[HDA_PARAM_CONN_LIST_LENGTH] = 0x01,			\
+		[HDA_PARAM_OUTPUT_AMP_CAP] = 0x00,	/* None */	\
+	},								\
+
+#define HDA_CODEC_INPUT_DESC						\
+	[HDA_CODEC_AUDIO_INPUT_NID] = {					\
+		[HDA_PARAM_AUDIO_WIDGET_CAP] =				\
+				HDA_CODEC_AUDIO_WCAP_INPUT |		\
+				HDA_CODEC_AUDIO_WCAP_CONN_LIST |	\
+				HDA_CODEC_AUDIO_WCAP_FORMAT_OVR |	\
+				HDA_CODEC_AUDIO_WCAP_AMP_OVR |		\
+				HDA_CODEC_AUDIO_WCAP_IN_AMP |		\
+				HDA_CODEC_AUDIO_WCAP_STEREO,		\
+		/* B16, 16.0 - 192.0kHz */				\
+		[HDA_PARAM_SUPP_PCM_SIZE_RATE] = (0x02 << 16) | 0x7fc,	\
+		[HDA_PARAM_SUPP_STREAM_FORMATS] =			\
+				HDA_CODEC_SUPP_STREAM_FORMATS_PCM,	\
+		[HDA_PARAM_OUTPUT_AMP_CAP] = 0x00,	/* None */	\
+		[HDA_PARAM_CONN_LIST_LENGTH] = 0x01,			\
+		[HDA_PARAM_INPUT_AMP_CAP] =				\
+				HDA_CODEC_OUTPUT_AMP_CAP_MUTE_CAP |	\
+				HDA_CODEC_OUTPUT_AMP_CAP_STEPSIZE |	\
+				HDA_CODEC_OUTPUT_AMP_CAP_NUMSTEPS |	\
+				HDA_CODEC_OUTPUT_AMP_CAP_OFFSET,	\
+	},								\
+	[HDA_CODEC_PIN_INPUT_NID] = {					\
+		[HDA_PARAM_AUDIO_WIDGET_CAP] =				\
+				HDA_CODEC_AUDIO_WCAP_PIN |		\
+				HDA_CODEC_AUDIO_WCAP_STEREO,		\
+		[HDA_PARAM_PIN_CAP] = HDA_CODEC_PIN_CAP_INPUT |		\
+				HDA_CODEC_PIN_CAP_PRESENCE_DETECT,	\
+		[HDA_PARAM_INPUT_AMP_CAP] = 0x00,	/* None */	\
+		[HDA_PARAM_OUTPUT_AMP_CAP] = 0x00,	/* None */	\
+	},								\
+
+static const uint32_t
+hda_codec_output_parameters[][HDA_CODEC_PARAMS_COUNT] = {
+	HDA_CODEC_ROOT_DESC
+	HDA_CODEC_FG_OUTPUT_DESC
+	HDA_CODEC_OUTPUT_DESC
+};
+
+static const uint32_t
+hda_codec_input_parameters[][HDA_CODEC_PARAMS_COUNT] = {
+	HDA_CODEC_ROOT_DESC
+	HDA_CODEC_FG_INPUT_DESC
+	HDA_CODEC_INPUT_DESC
+};
+
+static const uint32_t
+hda_codec_duplex_parameters[][HDA_CODEC_PARAMS_COUNT] = {
+	HDA_CODEC_ROOT_DESC
+	HDA_CODEC_FG_DUPLEX_DESC
+	HDA_CODEC_OUTPUT_DESC
+	HDA_CODEC_INPUT_DESC
+};
+
+#define HDA_CODEC_NODES_COUNT	(ARRAY_SIZE(hda_codec_duplex_parameters))
+
+static const uint8_t
+hda_codec_conn_list[HDA_CODEC_NODES_COUNT][HDA_CODEC_CONN_LIST_COUNT] = {
+	[HDA_CODEC_PIN_OUTPUT_NID] = {HDA_CODEC_AUDIO_OUTPUT_NID},
+	[HDA_CODEC_AUDIO_INPUT_NID] = {HDA_CODEC_PIN_INPUT_NID},
+};
+
+static const uint32_t
+hda_codec_conf_default[HDA_CODEC_NODES_COUNT] = {
+	[HDA_CODEC_PIN_OUTPUT_NID] =					\
+		HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_JACK |
+		HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_OUT |
+		HDA_CONFIG_DEFAULTCONF_COLOR_BLACK |
+		(0x01 << HDA_CONFIG_DEFAULTCONF_ASSOCIATION_SHIFT),
+	[HDA_CODEC_PIN_INPUT_NID] = HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_JACK |
+				    HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_IN |
+				    HDA_CONFIG_DEFAULTCONF_COLOR_RED |
+			(0x02 << HDA_CONFIG_DEFAULTCONF_ASSOCIATION_SHIFT),
+};
+
+static const uint8_t
+hda_codec_pin_ctrl_default[HDA_CODEC_NODES_COUNT] = {
+	[HDA_CODEC_PIN_OUTPUT_NID] = HDA_CODEC_PIN_WIDGET_CTRL_OUT_ENABLE,
+	[HDA_CODEC_PIN_INPUT_NID] = HDA_CODEC_PIN_WIDGET_CTRL_IN_ENABLE,
+};
+
+static const
+verb_func_t hda_codec_verb_handlers[HDA_CODEC_NODES_COUNT] = {
+	[HDA_CODEC_AUDIO_OUTPUT_NID] = hda_codec_audio_output_nid,
+	[HDA_CODEC_AUDIO_INPUT_NID] = hda_codec_audio_input_nid,
+};
+
+/*
+ * HDA Codec module function definitions
+ */
+
+static int
+hda_codec_init(struct hda_codec_inst *hci, const char *play,
+    const char *rec, const char *opts)
+{
+	struct hda_codec_softc *sc = NULL;
+	struct hda_codec_stream *st = NULL;
+	int err;
+
+	if (!(play || rec))
+		return (-1);
+
+	DPRINTF("cad: 0x%x opts: %s\n", hci->cad, opts);
+
+	sc = calloc(1, sizeof(*sc));
+	if (!sc)
+		return (-1);
+
+	if (play && rec)
+		sc->get_parameters = hda_codec_duplex_parameters;
+	else {
+		if (play)
+			sc->get_parameters = hda_codec_output_parameters;
+		else
+			sc->get_parameters = hda_codec_input_parameters;
+	}
+	sc->subsystem_id = HDA_CODEC_SUBSYSTEM_ID;
+	sc->no_nodes = HDA_CODEC_NODES_COUNT;
+	sc->conn_list = hda_codec_conn_list;
+	sc->conf_default = hda_codec_conf_default;
+	sc->pin_ctrl_default = hda_codec_pin_ctrl_default;
+	sc->verb_handlers = hda_codec_verb_handlers;
+	DPRINTF("HDA Codec nodes: %d\n", sc->no_nodes);
+
+	/*
+	 * Initialize the Audio Output stream
+	 */
+	if (play) {
+		st = &sc->streams[HDA_CODEC_STREAM_OUTPUT];
+
+		err = hda_audio_ctxt_init(&st->actx, "hda-audio-output",
+			hda_codec_audio_output_do_transfer,
+			hda_codec_audio_output_do_setup, sc);
+		assert(!err);
+
+		st->aud = audio_init(play, 1);
+		if (!st->aud) {
+			DPRINTF("Fail to init the output audio player\n");
+			return (-1);
+		}
+	}
+
+	/*
+	 * Initialize the Audio Input stream
+	 */
+	if (rec) {
+		st = &sc->streams[HDA_CODEC_STREAM_INPUT];
+
+		err = hda_audio_ctxt_init(&st->actx, "hda-audio-input",
+			hda_codec_audio_input_do_transfer,
+			hda_codec_audio_input_do_setup, sc);
+		assert(!err);
+
+		st->aud = audio_init(rec, 0);
+		if (!st->aud) {
+			DPRINTF("Fail to init the input audio player\n");
+			return (-1);
+		}
+	}
+
+	sc->hci = hci;
+	hci->priv = sc;
+
+	return (0);
+}
+
+static int
+hda_codec_reset(struct hda_codec_inst *hci)
+{
+	struct hda_ops *hops = NULL;
+	struct hda_codec_softc *sc = NULL;
+	struct hda_codec_stream *st = NULL;
+	int i;
+
+	assert(hci);
+
+	hops = hci->hops;
+	assert(hops);
+
+	sc = (struct hda_codec_softc *)hci->priv;
+	assert(sc);
+
+	for (i = 0; i < HDA_CODEC_STREAMS_COUNT; i++) {
+		st = &sc->streams[i];
+		st->left_gain = HDA_CODEC_AMP_NUMSTEPS;
+		st->right_gain = HDA_CODEC_AMP_NUMSTEPS;
+		st->left_mute = HDA_CODEC_SET_AMP_GAIN_MUTE_MUTE;
+		st->right_mute = HDA_CODEC_SET_AMP_GAIN_MUTE_MUTE;
+	}
+
+	DPRINTF("cad: 0x%x\n", hci->cad);
+
+	if (!hops->signal) {
+		DPRINTF("The controller ops does not implement \
+			 the signal function\n");
+		return (-1);
+	}
+
+	return (hops->signal(hci));
+}
+
+static int
+hda_codec_command(struct hda_codec_inst *hci, uint32_t cmd_data)
+{
+	struct hda_codec_softc *sc = NULL;
+	struct hda_ops *hops = NULL;
+	uint8_t cad = 0, nid = 0;
+	uint16_t verb = 0, payload = 0;
+	uint32_t res = 0;
+
+	/* 4 bits */
+	cad = (cmd_data >> HDA_CMD_CAD_SHIFT) & 0x0f;
+	/* 8 bits */
+	nid = (cmd_data >> HDA_CMD_NID_SHIFT) & 0xff;
+
+	if ((cmd_data & 0x70000) == 0x70000) {
+		/* 12 bits */
+		verb = (cmd_data >> HDA_CMD_VERB_12BIT_SHIFT) & 0x0fff;
+		/* 8 bits */
+		payload = cmd_data & 0xff;
+	} else {
+		/* 4 bits */
+		verb = (cmd_data >> HDA_CMD_VERB_4BIT_SHIFT) & 0x0f;
+		/* 16 bits */
+		payload = cmd_data & 0xffff;
+	}
+
+	assert(cad == hci->cad);
+	assert(hci);
+
+	hops = hci->hops;
+	assert(hops);
+
+	sc = (struct hda_codec_softc *)hci->priv;
+	assert(sc);
+
+	assert(nid < sc->no_nodes);
+
+	if (!hops->response) {
+		DPRINTF("The controller ops does not implement \
+			 the response function\n");
+		return (-1);
+	}
+
+	switch (verb) {
+	case HDA_CMD_VERB_GET_PARAMETER:
+		res = sc->get_parameters[nid][payload];
+		break;
+	case HDA_CMD_VERB_GET_CONN_LIST_ENTRY:
+		res = sc->conn_list[nid][0];
+		break;
+	case HDA_CMD_VERB_GET_PIN_WIDGET_CTRL:
+		res = sc->pin_ctrl_default[nid];
+		break;
+	case HDA_CMD_VERB_GET_PIN_SENSE:
+		res = HDA_CODEC_PIN_SENSE_PRESENCE_PLUGGED;
+		break;
+	case HDA_CMD_VERB_GET_CONFIGURATION_DEFAULT:
+		res = sc->conf_default[nid];
+		break;
+	case HDA_CMD_VERB_GET_SUBSYSTEM_ID:
+		res = sc->subsystem_id;
+		break;
+	default:
+		assert(sc->verb_handlers);
+		if (sc->verb_handlers[nid])
+			res = sc->verb_handlers[nid](sc, verb, payload);
+		else
+			DPRINTF("Unknown VERB: 0x%x\n", verb);
+		break;
+	}
+
+	DPRINTF("cad: 0x%x nid: 0x%x verb: 0x%x payload: 0x%x response: 0x%x\n",
+	    cad, nid, verb, payload, res);
+
+	return (hops->response(hci, res, HDA_CODEC_RESPONSE_EX_SOL));
+}
+
+static int
+hda_codec_notify(struct hda_codec_inst *hci, uint8_t run,
+    uint8_t stream, uint8_t dir)
+{
+	struct hda_codec_softc *sc = NULL;
+	struct hda_codec_stream *st = NULL;
+	struct hda_audio_ctxt *actx = NULL;
+	int i;
+	int err;
+
+	assert(hci);
+	assert(stream);
+
+	sc = (struct hda_codec_softc *)hci->priv;
+	assert(sc);
+
+	i = dir ? HDA_CODEC_STREAM_OUTPUT : HDA_CODEC_STREAM_INPUT;
+	st = &sc->streams[i];
+
+	DPRINTF("run: %d, stream: 0x%x, st->stream: 0x%x dir: %d\n",
+	    run, stream, st->stream, dir);

*** DIFF OUTPUT TRUNCATED AT 1000 LINES ***



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201906241931.x5OJVWVs025283>