Home | History | Annotate | Line # | Download | only in broadcom
bcm2835_vcaudio.c revision 1.19
      1  1.19   mlelstv /* $NetBSD: bcm2835_vcaudio.c,v 1.19 2023/04/30 14:20:23 mlelstv Exp $ */
      2   1.1  jmcneill 
      3   1.1  jmcneill /*-
      4   1.1  jmcneill  * Copyright (c) 2013 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. Redistributions in binary form must reproduce the above copyright
     13   1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14   1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15   1.1  jmcneill  *
     16   1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17   1.1  jmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18   1.1  jmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19   1.1  jmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20   1.1  jmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21   1.1  jmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22   1.1  jmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23   1.1  jmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24   1.1  jmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25   1.1  jmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26   1.1  jmcneill  * POSSIBILITY OF SUCH DAMAGE.
     27   1.1  jmcneill  */
     28   1.1  jmcneill 
     29   1.1  jmcneill /*
     30   1.1  jmcneill  * VideoCore audio interface
     31   1.1  jmcneill  */
     32   1.1  jmcneill 
     33   1.1  jmcneill #include <sys/cdefs.h>
     34  1.19   mlelstv __KERNEL_RCSID(0, "$NetBSD: bcm2835_vcaudio.c,v 1.19 2023/04/30 14:20:23 mlelstv Exp $");
     35   1.1  jmcneill 
     36   1.1  jmcneill #include <sys/param.h>
     37   1.1  jmcneill #include <sys/types.h>
     38   1.1  jmcneill #include <sys/systm.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 #include <sys/kmem.h>
     43   1.1  jmcneill 
     44   1.1  jmcneill #include <sys/audioio.h>
     45  1.14     isaki #include <dev/audio/audio_if.h>
     46   1.1  jmcneill 
     47   1.1  jmcneill #include <interface/compat/vchi_bsd.h>
     48   1.1  jmcneill #include <interface/vchiq_arm/vchiq_netbsd.h>
     49   1.1  jmcneill #include <interface/vchi/vchi.h>
     50   1.1  jmcneill 
     51   1.1  jmcneill #include "bcm2835_vcaudioreg.h"
     52   1.1  jmcneill 
     53   1.9  jmcneill /* levels with 5% volume step */
     54   1.9  jmcneill static int vcaudio_levels[] = {
     55   1.9  jmcneill 	-10239, -4605, -3794, -3218, -2772,
     56   1.9  jmcneill 	-2407, -2099, -1832, -1597, -1386,
     57   1.9  jmcneill 	-1195, -1021, -861, -713, -575,
     58   1.9  jmcneill 	-446, -325, -210, -102, 0,
     59   1.9  jmcneill };
     60   1.9  jmcneill 
     61   1.9  jmcneill #define vol2db(vol)	vcaudio_levels[((vol) * 20) >> 8]
     62   1.9  jmcneill #define vol2vc(vol)	((uint32_t)(-(vol2db((vol)) << 8) / 100))
     63   1.1  jmcneill 
     64   1.1  jmcneill enum {
     65   1.1  jmcneill 	VCAUDIO_OUTPUT_CLASS,
     66   1.1  jmcneill 	VCAUDIO_INPUT_CLASS,
     67   1.1  jmcneill 	VCAUDIO_OUTPUT_MASTER_VOLUME,
     68   1.1  jmcneill 	VCAUDIO_INPUT_DAC_VOLUME,
     69   1.9  jmcneill 	VCAUDIO_OUTPUT_AUTO_VOLUME,
     70   1.8  jmcneill 	VCAUDIO_OUTPUT_HEADPHONE_VOLUME,
     71   1.9  jmcneill 	VCAUDIO_OUTPUT_HDMI_VOLUME,
     72   1.8  jmcneill 	VCAUDIO_OUTPUT_SELECT,
     73   1.1  jmcneill 	VCAUDIO_ENUM_LAST,
     74   1.1  jmcneill };
     75   1.1  jmcneill 
     76   1.1  jmcneill enum vcaudio_dest {
     77   1.1  jmcneill 	VCAUDIO_DEST_AUTO = 0,
     78   1.1  jmcneill 	VCAUDIO_DEST_HP = 1,
     79   1.1  jmcneill 	VCAUDIO_DEST_HDMI = 2,
     80   1.1  jmcneill };
     81   1.1  jmcneill 
     82   1.7     skrll /*
     83  1.10     skrll  * Maximum message size is 4000 bytes and VCHIQ can accept 16 messages.
     84   1.7     skrll  *
     85   1.7     skrll  * 4000 bytes of 16bit 48kHz stereo is approximately 21ms.
     86   1.7     skrll  *
     87   1.7     skrll  * We get complete messages at ~10ms intervals.
     88   1.7     skrll  *
     89  1.10     skrll  * Setting blocksize to 4 x 1600 means that we send approx 33ms of audio. We
     90  1.10     skrll  * prefill by two blocks before starting audio meaning we have 50ms of latency.
     91  1.10     skrll  *
     92  1.10     skrll  * Six messages of 1600 bytes was chosen working back from a desired latency of
     93  1.10     skrll  * 50ms.
     94   1.7     skrll  */
     95   1.7     skrll 
     96  1.14     isaki /* 40ms block of 16bit 48kHz stereo is 7680 bytes. */
     97  1.14     isaki #define VCAUDIO_MSGSIZE		1920
     98  1.10     skrll #define VCAUDIO_NUMMSGS		4
     99   1.7     skrll #define VCAUDIO_BLOCKSIZE	(VCAUDIO_MSGSIZE * VCAUDIO_NUMMSGS)
    100  1.14     isaki /* The driver seems to have no buffer size restrictions. */
    101   1.7     skrll #define VCAUDIO_PREFILLCOUNT	2
    102   1.1  jmcneill 
    103   1.1  jmcneill struct vcaudio_softc {
    104   1.1  jmcneill 	device_t			sc_dev;
    105   1.1  jmcneill 	device_t			sc_audiodev;
    106   1.1  jmcneill 
    107   1.7     skrll 	lwp_t				*sc_lwp;
    108   1.7     skrll 
    109   1.1  jmcneill 	kmutex_t			sc_lock;
    110   1.1  jmcneill 	kmutex_t			sc_intr_lock;
    111   1.7     skrll 	kcondvar_t			sc_datacv;
    112   1.3     skrll 
    113   1.3     skrll 	kmutex_t			sc_msglock;
    114   1.3     skrll 	kcondvar_t			sc_msgcv;
    115   1.1  jmcneill 
    116   1.1  jmcneill 	struct audio_format		sc_format;
    117   1.1  jmcneill 
    118   1.1  jmcneill 	void				(*sc_pint)(void *);
    119   1.1  jmcneill 	void				*sc_pintarg;
    120   1.1  jmcneill 	audio_params_t			sc_pparam;
    121   1.1  jmcneill 	bool				sc_started;
    122   1.7     skrll 	int				sc_pblkcnt;	// prefill block count
    123   1.7     skrll 	int				sc_abytes;	// available bytes
    124   1.7     skrll 	int				sc_pbytes;	// played bytes
    125   1.1  jmcneill 	off_t				sc_ppos;
    126   1.1  jmcneill 	void				*sc_pstart;
    127   1.1  jmcneill 	void				*sc_pend;
    128   1.1  jmcneill 	int				sc_pblksize;
    129   1.1  jmcneill 
    130   1.3     skrll 	bool				sc_msgdone;
    131   1.1  jmcneill 	int				sc_success;
    132   1.1  jmcneill 
    133   1.1  jmcneill 	VCHI_INSTANCE_T			sc_instance;
    134   1.1  jmcneill 	VCHI_CONNECTION_T		sc_connection;
    135   1.1  jmcneill 	VCHI_SERVICE_HANDLE_T		sc_service;
    136   1.1  jmcneill 
    137   1.6  jmcneill 	short				sc_peer_version;
    138   1.6  jmcneill 
    139   1.9  jmcneill 	int				sc_hwvol[3];
    140   1.1  jmcneill 	enum vcaudio_dest		sc_dest;
    141   1.8  jmcneill 
    142   1.8  jmcneill 	uint8_t				sc_swvol;
    143   1.1  jmcneill };
    144   1.1  jmcneill 
    145   1.1  jmcneill static int	vcaudio_match(device_t, cfdata_t, void *);
    146   1.1  jmcneill static void	vcaudio_attach(device_t, device_t, void *);
    147   1.1  jmcneill static int	vcaudio_rescan(device_t, const char *, const int *);
    148   1.1  jmcneill static void	vcaudio_childdet(device_t, device_t);
    149   1.1  jmcneill 
    150   1.1  jmcneill static int	vcaudio_init(struct vcaudio_softc *);
    151   1.1  jmcneill static void	vcaudio_service_callback(void *,
    152   1.7     skrll     const VCHI_CALLBACK_REASON_T, void *);
    153   1.7     skrll static int	vcaudio_msg_sync(struct vcaudio_softc *, VC_AUDIO_MSG_T *,
    154   1.7     skrll     size_t);
    155   1.7     skrll static void	vcaudio_worker(void *);
    156   1.1  jmcneill 
    157  1.14     isaki static int	vcaudio_query_format(void *, audio_format_query_t *);
    158  1.14     isaki static int	vcaudio_set_format(void *, int,
    159  1.14     isaki     const audio_params_t *, const audio_params_t *,
    160  1.14     isaki     audio_filter_reg_t *, audio_filter_reg_t *);
    161   1.1  jmcneill static int	vcaudio_halt_output(void *);
    162   1.1  jmcneill static int	vcaudio_set_port(void *, mixer_ctrl_t *);
    163   1.1  jmcneill static int	vcaudio_get_port(void *, mixer_ctrl_t *);
    164   1.1  jmcneill static int	vcaudio_query_devinfo(void *, mixer_devinfo_t *);
    165   1.1  jmcneill static int	vcaudio_getdev(void *, struct audio_device *);
    166   1.1  jmcneill static int	vcaudio_get_props(void *);
    167   1.7     skrll 
    168   1.7     skrll static int	vcaudio_round_blocksize(void *, int, int,
    169   1.7     skrll     const audio_params_t *);
    170   1.7     skrll 
    171   1.1  jmcneill static int	vcaudio_trigger_output(void *, void *, void *, int,
    172   1.7     skrll     void (*)(void *), void *, const audio_params_t *);
    173   1.7     skrll 
    174   1.1  jmcneill static void	vcaudio_get_locks(void *, kmutex_t **, kmutex_t **);
    175   1.1  jmcneill 
    176  1.14     isaki static void vcaudio_swvol_codec(audio_filter_arg_t *);
    177   1.8  jmcneill 
    178   1.1  jmcneill static const struct audio_hw_if vcaudio_hw_if = {
    179  1.14     isaki 	.query_format = vcaudio_query_format,
    180  1.14     isaki 	.set_format = vcaudio_set_format,
    181   1.1  jmcneill 	.halt_output = vcaudio_halt_output,
    182   1.1  jmcneill 	.getdev = vcaudio_getdev,
    183   1.1  jmcneill 	.set_port = vcaudio_set_port,
    184   1.1  jmcneill 	.get_port = vcaudio_get_port,
    185   1.1  jmcneill 	.query_devinfo = vcaudio_query_devinfo,
    186   1.1  jmcneill 	.get_props = vcaudio_get_props,
    187   1.1  jmcneill 	.round_blocksize = vcaudio_round_blocksize,
    188   1.1  jmcneill 	.trigger_output = vcaudio_trigger_output,
    189   1.1  jmcneill 	.get_locks = vcaudio_get_locks,
    190   1.1  jmcneill };
    191   1.1  jmcneill 
    192   1.1  jmcneill CFATTACH_DECL2_NEW(vcaudio, sizeof(struct vcaudio_softc),
    193   1.7     skrll     vcaudio_match, vcaudio_attach, NULL, NULL, vcaudio_rescan,
    194   1.7     skrll     vcaudio_childdet);
    195   1.1  jmcneill 
    196   1.1  jmcneill static int
    197   1.1  jmcneill vcaudio_match(device_t parent, cfdata_t match, void *aux)
    198   1.1  jmcneill {
    199   1.1  jmcneill 	struct vchiq_attach_args *vaa = aux;
    200   1.1  jmcneill 
    201   1.1  jmcneill 	return !strcmp(vaa->vaa_name, "AUDS");
    202   1.1  jmcneill }
    203   1.1  jmcneill 
    204   1.1  jmcneill static void
    205   1.1  jmcneill vcaudio_attach(device_t parent, device_t self, void *aux)
    206   1.1  jmcneill {
    207   1.1  jmcneill 	struct vcaudio_softc *sc = device_private(self);
    208   1.1  jmcneill 	int error;
    209   1.1  jmcneill 
    210   1.1  jmcneill 	sc->sc_dev = self;
    211   1.1  jmcneill 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    212   1.3     skrll 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_NONE);
    213   1.3     skrll 	mutex_init(&sc->sc_msglock, MUTEX_DEFAULT, IPL_NONE);
    214   1.7     skrll 	cv_init(&sc->sc_msgcv, "msg");
    215   1.7     skrll 	cv_init(&sc->sc_datacv, "data");
    216   1.3     skrll 	sc->sc_success = -1;
    217   1.7     skrll 
    218   1.7     skrll 	error = kthread_create(PRI_BIO, KTHREAD_MPSAFE, NULL, vcaudio_worker,
    219   1.7     skrll 	    sc, &sc->sc_lwp, "vcaudio");
    220   1.1  jmcneill 	if (error) {
    221   1.7     skrll 		aprint_error(": couldn't create thread (%d)\n", error);
    222   1.1  jmcneill 		return;
    223   1.1  jmcneill 	}
    224   1.1  jmcneill 
    225   1.1  jmcneill 	aprint_naive("\n");
    226   1.7     skrll 	aprint_normal(": auds\n");
    227   1.7     skrll 
    228   1.7     skrll 	error = vcaudio_rescan(self, NULL, NULL);
    229   1.7     skrll 	if (error)
    230   1.7     skrll 		aprint_error_dev(self, "not configured\n");
    231   1.1  jmcneill 
    232   1.1  jmcneill }
    233   1.1  jmcneill 
    234   1.1  jmcneill static int
    235   1.1  jmcneill vcaudio_rescan(device_t self, const char *ifattr, const int *locs)
    236   1.1  jmcneill {
    237   1.1  jmcneill 	struct vcaudio_softc *sc = device_private(self);
    238   1.6  jmcneill 	int error;
    239   1.1  jmcneill 
    240  1.18   thorpej 	if (sc->sc_audiodev == NULL) {
    241   1.6  jmcneill 		error = vcaudio_init(sc);
    242   1.7     skrll 		if (error) {
    243   1.6  jmcneill 			return error;
    244   1.7     skrll 		}
    245   1.6  jmcneill 
    246   1.1  jmcneill 		sc->sc_audiodev = audio_attach_mi(&vcaudio_hw_if,
    247   1.1  jmcneill 		    sc, sc->sc_dev);
    248   1.1  jmcneill 	}
    249   1.1  jmcneill 	return 0;
    250   1.1  jmcneill }
    251   1.1  jmcneill 
    252   1.1  jmcneill static void
    253   1.1  jmcneill vcaudio_childdet(device_t self, device_t child)
    254   1.1  jmcneill {
    255   1.1  jmcneill 	struct vcaudio_softc *sc = device_private(self);
    256   1.1  jmcneill 
    257   1.1  jmcneill 	if (sc->sc_audiodev == child)
    258   1.1  jmcneill 		sc->sc_audiodev = NULL;
    259   1.1  jmcneill }
    260   1.1  jmcneill 
    261   1.1  jmcneill static int
    262   1.1  jmcneill vcaudio_init(struct vcaudio_softc *sc)
    263   1.1  jmcneill {
    264   1.1  jmcneill 	VC_AUDIO_MSG_T msg;
    265   1.1  jmcneill 	int error;
    266   1.1  jmcneill 
    267   1.8  jmcneill 	sc->sc_swvol = 255;
    268   1.9  jmcneill 	sc->sc_hwvol[VCAUDIO_DEST_AUTO] = 255;
    269   1.9  jmcneill 	sc->sc_hwvol[VCAUDIO_DEST_HP] = 255;
    270   1.9  jmcneill 	sc->sc_hwvol[VCAUDIO_DEST_HDMI] = 255;
    271   1.1  jmcneill 	sc->sc_dest = VCAUDIO_DEST_AUTO;
    272   1.1  jmcneill 
    273  1.17       nia 	sc->sc_format.mode = AUMODE_PLAY;
    274   1.1  jmcneill 	sc->sc_format.encoding = AUDIO_ENCODING_SLINEAR_LE;
    275   1.1  jmcneill 	sc->sc_format.validbits = 16;
    276   1.1  jmcneill 	sc->sc_format.precision = 16;
    277   1.1  jmcneill 	sc->sc_format.channels = 2;
    278   1.1  jmcneill 	sc->sc_format.channel_mask = AUFMT_STEREO;
    279   1.1  jmcneill 	sc->sc_format.frequency_type = 0;
    280   1.4  jmcneill 	sc->sc_format.frequency[0] = 48000;
    281   1.1  jmcneill 	sc->sc_format.frequency[1] = 48000;
    282   1.2     skrll 
    283   1.1  jmcneill 	error = vchi_initialise(&sc->sc_instance);
    284   1.1  jmcneill 	if (error) {
    285   1.7     skrll 		aprint_error_dev(sc->sc_dev,
    286   1.7     skrll 		    "couldn't init vchi instance (%d)\n", error);
    287   1.1  jmcneill 		return EIO;
    288   1.1  jmcneill 	}
    289   1.1  jmcneill 
    290   1.1  jmcneill 	error = vchi_connect(NULL, 0, sc->sc_instance);
    291   1.1  jmcneill 	if (error) {
    292   1.7     skrll 		aprint_error_dev(sc->sc_dev,
    293   1.7     skrll 		    "couldn't connect vchi (%d)\n", error);
    294   1.1  jmcneill 		return EIO;
    295   1.1  jmcneill 	}
    296   1.1  jmcneill 
    297   1.1  jmcneill 	SERVICE_CREATION_T setup = {
    298   1.1  jmcneill 		.version = VCHI_VERSION(VC_AUDIOSERV_VER),
    299   1.1  jmcneill 		.service_id = VC_AUDIO_SERVER_NAME,
    300   1.1  jmcneill 		.connection = &sc->sc_connection,
    301   1.1  jmcneill 		.rx_fifo_size = 0,
    302   1.1  jmcneill 		.tx_fifo_size = 0,
    303   1.1  jmcneill 		.callback = vcaudio_service_callback,
    304   1.1  jmcneill 		.callback_param = sc,
    305   1.1  jmcneill 		.want_unaligned_bulk_rx = 1,
    306   1.1  jmcneill 		.want_unaligned_bulk_tx = 1,
    307   1.1  jmcneill 		.want_crc = 0,
    308   1.1  jmcneill 	};
    309   1.1  jmcneill 
    310   1.1  jmcneill 	error = vchi_service_open(sc->sc_instance, &setup, &sc->sc_service);
    311   1.1  jmcneill 	if (error) {
    312   1.7     skrll 		aprint_error_dev(sc->sc_dev, "couldn't open service (%d)\n",
    313   1.1  jmcneill 		    error);
    314   1.1  jmcneill 		return EIO;
    315   1.1  jmcneill 	}
    316   1.1  jmcneill 
    317   1.6  jmcneill 	vchi_get_peer_version(sc->sc_service, &sc->sc_peer_version);
    318   1.6  jmcneill 
    319   1.6  jmcneill 	if (sc->sc_peer_version < 2) {
    320   1.6  jmcneill 		aprint_error_dev(sc->sc_dev,
    321   1.6  jmcneill 		    "peer version (%d) is less than the required version (2)\n",
    322   1.6  jmcneill 		    sc->sc_peer_version);
    323   1.6  jmcneill 		return EINVAL;
    324   1.6  jmcneill 	}
    325   1.6  jmcneill 
    326   1.1  jmcneill 	memset(&msg, 0, sizeof(msg));
    327   1.1  jmcneill 	msg.type = VC_AUDIO_MSG_TYPE_OPEN;
    328   1.1  jmcneill 	error = vchi_msg_queue(sc->sc_service, &msg, sizeof(msg),
    329   1.1  jmcneill 	    VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
    330   1.1  jmcneill 	if (error) {
    331   1.7     skrll 		aprint_error_dev(sc->sc_dev,
    332   1.7     skrll 		    "couldn't send OPEN message (%d)\n", error);
    333   1.1  jmcneill 	}
    334   1.1  jmcneill 
    335   1.1  jmcneill 	memset(&msg, 0, sizeof(msg));
    336   1.4  jmcneill 	msg.type = VC_AUDIO_MSG_TYPE_CONFIG;
    337   1.4  jmcneill 	msg.u.config.channels = 2;
    338   1.4  jmcneill 	msg.u.config.samplerate = 48000;
    339   1.4  jmcneill 	msg.u.config.bps = 16;
    340   1.4  jmcneill 	error = vcaudio_msg_sync(sc, &msg, sizeof(msg));
    341   1.4  jmcneill 	if (error) {
    342   1.7     skrll 		aprint_error_dev(sc->sc_dev,
    343   1.7     skrll 		    "couldn't send CONFIG message (%d)\n", error);
    344   1.4  jmcneill 	}
    345   1.4  jmcneill 
    346   1.4  jmcneill 	memset(&msg, 0, sizeof(msg));
    347   1.1  jmcneill 	msg.type = VC_AUDIO_MSG_TYPE_CONTROL;
    348   1.9  jmcneill 	msg.u.control.volume = vol2vc(sc->sc_hwvol[sc->sc_dest]);
    349   1.8  jmcneill 	msg.u.control.dest = sc->sc_dest;
    350   1.1  jmcneill 	error = vcaudio_msg_sync(sc, &msg, sizeof(msg));
    351   1.1  jmcneill 	if (error) {
    352   1.7     skrll 		aprint_error_dev(sc->sc_dev,
    353   1.7     skrll 		    "couldn't send CONTROL message (%d)\n", error);
    354   1.1  jmcneill 	}
    355   1.7     skrll 
    356   1.1  jmcneill 	vchi_service_release(sc->sc_service);
    357   1.1  jmcneill 
    358   1.1  jmcneill 	return 0;
    359   1.1  jmcneill }
    360   1.1  jmcneill 
    361   1.1  jmcneill static void
    362   1.1  jmcneill vcaudio_service_callback(void *priv, const VCHI_CALLBACK_REASON_T reason,
    363   1.1  jmcneill     void *msg_handle)
    364   1.1  jmcneill {
    365   1.1  jmcneill 	struct vcaudio_softc *sc = priv;
    366   1.1  jmcneill 	VC_AUDIO_MSG_T msg;
    367   1.1  jmcneill 	int32_t msglen = 0;
    368   1.1  jmcneill 	int error;
    369   1.1  jmcneill 
    370   1.1  jmcneill 	if (sc == NULL || reason != VCHI_CALLBACK_MSG_AVAILABLE)
    371   1.1  jmcneill 		return;
    372   1.1  jmcneill 
    373   1.1  jmcneill 	memset(&msg, 0, sizeof(msg));
    374   1.1  jmcneill 	error = vchi_msg_dequeue(sc->sc_service, &msg, sizeof(msg), &msglen,
    375   1.1  jmcneill 	    VCHI_FLAGS_NONE);
    376   1.1  jmcneill 	if (error) {
    377   1.1  jmcneill 		device_printf(sc->sc_dev, "couldn't dequeue msg (%d)\n",
    378   1.1  jmcneill 		    error);
    379   1.1  jmcneill 		return;
    380   1.1  jmcneill 	}
    381   1.1  jmcneill 
    382   1.1  jmcneill 	switch (msg.type) {
    383   1.1  jmcneill 	case VC_AUDIO_MSG_TYPE_RESULT:
    384   1.3     skrll 		mutex_enter(&sc->sc_msglock);
    385   1.1  jmcneill 		sc->sc_success = msg.u.result.success;
    386   1.3     skrll 		sc->sc_msgdone = true;
    387   1.3     skrll 		cv_broadcast(&sc->sc_msgcv);
    388   1.3     skrll 		mutex_exit(&sc->sc_msglock);
    389   1.1  jmcneill 		break;
    390   1.7     skrll 
    391   1.1  jmcneill 	case VC_AUDIO_MSG_TYPE_COMPLETE:
    392  1.15   mlelstv 		if (msg.u.complete.cookie1 == VC_AUDIO_WRITE_COOKIE1 &&
    393  1.15   mlelstv 		    msg.u.complete.cookie2 == VC_AUDIO_WRITE_COOKIE2) {
    394   1.5  jmcneill 			int count = msg.u.complete.count & 0xffff;
    395   1.7     skrll 			int perr = (msg.u.complete.count & __BIT(30)) != 0;
    396   1.5  jmcneill 			bool sched = false;
    397   1.7     skrll 
    398   1.3     skrll 			mutex_enter(&sc->sc_intr_lock);
    399   1.7     skrll 
    400   1.5  jmcneill 			if (count > 0) {
    401   1.5  jmcneill 				sc->sc_pbytes += count;
    402   1.5  jmcneill 			}
    403   1.5  jmcneill 			if (perr && sc->sc_started) {
    404   1.5  jmcneill #ifdef VCAUDIO_DEBUG
    405   1.5  jmcneill 				device_printf(sc->sc_dev, "underrun\n");
    406   1.5  jmcneill #endif
    407   1.5  jmcneill 				sched = true;
    408   1.1  jmcneill 			}
    409   1.1  jmcneill 			if (sc->sc_pbytes >= sc->sc_pblksize) {
    410   1.1  jmcneill 				sc->sc_pbytes -= sc->sc_pblksize;
    411   1.5  jmcneill 				sched = true;
    412   1.5  jmcneill 			}
    413   1.5  jmcneill 
    414  1.11       nat 			if (sched && sc->sc_pint) {
    415  1.15   mlelstv 				sc->sc_pint(sc->sc_pintarg);
    416   1.7     skrll 				sc->sc_abytes += sc->sc_pblksize;
    417   1.7     skrll 				cv_signal(&sc->sc_datacv);
    418   1.1  jmcneill 			}
    419   1.3     skrll 			mutex_exit(&sc->sc_intr_lock);
    420   1.1  jmcneill 		}
    421   1.1  jmcneill 		break;
    422   1.1  jmcneill 	default:
    423   1.1  jmcneill 		break;
    424   1.1  jmcneill 	}
    425   1.1  jmcneill }
    426   1.1  jmcneill 
    427   1.1  jmcneill static void
    428   1.7     skrll vcaudio_worker(void *priv)
    429   1.1  jmcneill {
    430   1.1  jmcneill 	struct vcaudio_softc *sc = priv;
    431   1.1  jmcneill 	VC_AUDIO_MSG_T msg;
    432   1.1  jmcneill 	void (*intr)(void *);
    433   1.1  jmcneill 	void *intrarg;
    434   1.1  jmcneill 	void *block;
    435   1.1  jmcneill 	int error, resid, off, nb, count;
    436   1.1  jmcneill 
    437   1.1  jmcneill 	mutex_enter(&sc->sc_intr_lock);
    438   1.1  jmcneill 
    439   1.7     skrll 	while (true) {
    440   1.7     skrll 		intr = sc->sc_pint;
    441   1.7     skrll 		intrarg = sc->sc_pintarg;
    442   1.7     skrll 
    443   1.7     skrll 		if (intr == NULL || intrarg == NULL) {
    444   1.7     skrll 			cv_wait_sig(&sc->sc_datacv, &sc->sc_intr_lock);
    445   1.7     skrll 			continue;
    446   1.7     skrll 		}
    447   1.1  jmcneill 
    448   1.7     skrll 		KASSERT(sc->sc_pblksize != 0);
    449   1.1  jmcneill 
    450   1.7     skrll 		if (sc->sc_abytes < sc->sc_pblksize) {
    451   1.7     skrll 			cv_wait_sig(&sc->sc_datacv, &sc->sc_intr_lock);
    452   1.7     skrll 			continue;
    453   1.7     skrll 		}
    454   1.7     skrll 		count = sc->sc_pblksize;
    455   1.1  jmcneill 
    456   1.1  jmcneill 		memset(&msg, 0, sizeof(msg));
    457   1.7     skrll 		msg.type = VC_AUDIO_MSG_TYPE_WRITE;
    458   1.7     skrll 		msg.u.write.max_packet = VCAUDIO_MSGSIZE;
    459   1.7     skrll 		msg.u.write.count = count;
    460  1.15   mlelstv 		msg.u.write.cookie1 = VC_AUDIO_WRITE_COOKIE1;
    461  1.15   mlelstv 		msg.u.write.cookie2 = VC_AUDIO_WRITE_COOKIE2;
    462   1.7     skrll 		msg.u.write.silence = 0;
    463   1.7     skrll 
    464  1.13     isaki 		block = (uint8_t *)sc->sc_pstart + sc->sc_ppos;
    465   1.7     skrll 		resid = count;
    466   1.7     skrll 		off = 0;
    467   1.7     skrll 
    468   1.7     skrll 		vchi_service_use(sc->sc_service);
    469   1.7     skrll 
    470   1.1  jmcneill 		error = vchi_msg_queue(sc->sc_service, &msg, sizeof(msg),
    471   1.1  jmcneill 		    VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
    472   1.1  jmcneill 		if (error) {
    473   1.7     skrll 			printf("%s: failed to write (%d)\n", __func__, error);
    474   1.1  jmcneill 			goto done;
    475   1.1  jmcneill 		}
    476   1.1  jmcneill 
    477   1.7     skrll 		while (resid > 0) {
    478  1.12  riastrad 			nb = uimin(resid, msg.u.write.max_packet);
    479   1.7     skrll 			error = vchi_msg_queue(sc->sc_service,
    480   1.7     skrll 			    (char *)block + off, nb,
    481   1.7     skrll 			    VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
    482   1.7     skrll 			if (error) {
    483   1.7     skrll 				/* XXX What to do here? */
    484   1.7     skrll 				device_printf(sc->sc_dev,
    485   1.7     skrll 				    "failed to queue data (%d)\n", error);
    486   1.7     skrll 				goto done;
    487   1.7     skrll 			}
    488   1.7     skrll 			off += nb;
    489   1.7     skrll 			resid -= nb;
    490   1.7     skrll 		}
    491   1.1  jmcneill 
    492   1.7     skrll 		sc->sc_abytes -= count;
    493   1.7     skrll 		sc->sc_ppos += count;
    494   1.7     skrll 		if ((uint8_t *)sc->sc_pstart + sc->sc_ppos >=
    495   1.7     skrll 		    (uint8_t *)sc->sc_pend)
    496   1.7     skrll 			sc->sc_ppos = 0;
    497   1.7     skrll 
    498   1.7     skrll 		if (!sc->sc_started) {
    499  1.13     isaki 			++sc->sc_pblkcnt;
    500   1.7     skrll 
    501   1.7     skrll 			if (sc->sc_pblkcnt == VCAUDIO_PREFILLCOUNT) {
    502   1.7     skrll 
    503   1.7     skrll 				memset(&msg, 0, sizeof(msg));
    504   1.7     skrll 				msg.type = VC_AUDIO_MSG_TYPE_START;
    505   1.7     skrll 				error = vchi_msg_queue(sc->sc_service, &msg,
    506   1.7     skrll 				    sizeof(msg), VCHI_FLAGS_BLOCK_UNTIL_QUEUED,
    507   1.7     skrll 				    NULL);
    508   1.7     skrll 				if (error) {
    509   1.7     skrll 					device_printf(sc->sc_dev,
    510   1.7     skrll 					    "failed to start (%d)\n", error);
    511   1.7     skrll 					goto done;
    512   1.7     skrll 				}
    513   1.7     skrll 				sc->sc_started = true;
    514   1.7     skrll 				sc->sc_pbytes = 0;
    515   1.7     skrll 			} else {
    516   1.7     skrll 				intr(intrarg);
    517   1.7     skrll 				sc->sc_abytes += sc->sc_pblksize;
    518   1.7     skrll 			}
    519   1.1  jmcneill 		}
    520   1.1  jmcneill 
    521   1.1  jmcneill done:
    522   1.7     skrll 		vchi_service_release(sc->sc_service);
    523   1.7     skrll 	}
    524   1.1  jmcneill }
    525   1.1  jmcneill 
    526   1.1  jmcneill static int
    527   1.1  jmcneill vcaudio_msg_sync(struct vcaudio_softc *sc, VC_AUDIO_MSG_T *msg, size_t msglen)
    528   1.1  jmcneill {
    529   1.1  jmcneill 	int error = 0;
    530   1.1  jmcneill 
    531   1.3     skrll 	mutex_enter(&sc->sc_msglock);
    532   1.3     skrll 
    533   1.1  jmcneill 	sc->sc_success = -1;
    534   1.3     skrll 	sc->sc_msgdone = false;
    535   1.3     skrll 
    536   1.1  jmcneill 	error = vchi_msg_queue(sc->sc_service, msg, msglen,
    537   1.1  jmcneill 	    VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
    538   1.1  jmcneill 	if (error) {
    539   1.1  jmcneill 		printf("%s: failed to queue message (%d)\n", __func__, error);
    540   1.1  jmcneill 		goto done;
    541   1.1  jmcneill 	}
    542   1.3     skrll 
    543   1.3     skrll 	while (!sc->sc_msgdone) {
    544   1.3     skrll 		error = cv_wait_sig(&sc->sc_msgcv, &sc->sc_msglock);
    545   1.1  jmcneill 		if (error)
    546   1.1  jmcneill 			break;
    547   1.1  jmcneill 	}
    548   1.3     skrll 	if (sc->sc_success != 0)
    549   1.1  jmcneill 		error = EIO;
    550   1.1  jmcneill done:
    551   1.3     skrll 	mutex_exit(&sc->sc_msglock);
    552   1.1  jmcneill 
    553   1.1  jmcneill 	return error;
    554   1.1  jmcneill }
    555   1.1  jmcneill 
    556   1.1  jmcneill static int
    557  1.14     isaki vcaudio_query_format(void *priv, audio_format_query_t *afp)
    558   1.1  jmcneill {
    559   1.1  jmcneill 	struct vcaudio_softc *sc = priv;
    560   1.1  jmcneill 
    561  1.14     isaki 	return audio_query_format(&sc->sc_format, 1, afp);
    562   1.1  jmcneill }
    563   1.1  jmcneill 
    564   1.1  jmcneill static int
    565  1.14     isaki vcaudio_set_format(void *priv, int setmode,
    566  1.14     isaki     const audio_params_t *play, const audio_params_t *rec,
    567  1.14     isaki     audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
    568   1.1  jmcneill {
    569   1.1  jmcneill 	struct vcaudio_softc *sc = priv;
    570   1.1  jmcneill 
    571  1.14     isaki 	pfil->codec = vcaudio_swvol_codec;
    572  1.14     isaki 	pfil->context = sc;
    573   1.1  jmcneill 
    574   1.1  jmcneill 	return 0;
    575   1.1  jmcneill }
    576   1.1  jmcneill 
    577   1.1  jmcneill static int
    578   1.1  jmcneill vcaudio_halt_output(void *priv)
    579   1.1  jmcneill {
    580   1.1  jmcneill 	struct vcaudio_softc *sc = priv;
    581   1.1  jmcneill 	VC_AUDIO_MSG_T msg;
    582   1.1  jmcneill 	int error = 0;
    583   1.1  jmcneill 
    584   1.7     skrll 	KASSERT(mutex_owned(&sc->sc_intr_lock));
    585   1.7     skrll 
    586   1.1  jmcneill 	vchi_service_use(sc->sc_service);
    587   1.1  jmcneill 	memset(&msg, 0, sizeof(msg));
    588   1.1  jmcneill 	msg.type = VC_AUDIO_MSG_TYPE_STOP;
    589   1.7     skrll 	msg.u.stop.draining = 0;
    590   1.1  jmcneill 	error = vchi_msg_queue(sc->sc_service, &msg, sizeof(msg),
    591   1.1  jmcneill 	    VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
    592   1.1  jmcneill 	if (error) {
    593   1.1  jmcneill 		device_printf(sc->sc_dev, "couldn't send STOP message (%d)\n",
    594   1.1  jmcneill 		    error);
    595   1.1  jmcneill 	}
    596   1.1  jmcneill 	vchi_service_release(sc->sc_service);
    597   1.1  jmcneill 
    598   1.1  jmcneill 	sc->sc_pint = NULL;
    599   1.1  jmcneill 	sc->sc_pintarg = NULL;
    600   1.1  jmcneill 	sc->sc_started = false;
    601   1.1  jmcneill 
    602   1.1  jmcneill #ifdef VCAUDIO_DEBUG
    603   1.1  jmcneill 	device_printf(sc->sc_dev, "halting output\n");
    604   1.1  jmcneill #endif
    605   1.1  jmcneill 
    606   1.1  jmcneill 	return error;
    607   1.1  jmcneill }
    608   1.1  jmcneill 
    609   1.1  jmcneill static int
    610   1.9  jmcneill vcaudio_set_volume(struct vcaudio_softc *sc, enum vcaudio_dest dest,
    611   1.9  jmcneill     int hwvol)
    612   1.9  jmcneill {
    613   1.9  jmcneill 	VC_AUDIO_MSG_T msg;
    614   1.9  jmcneill 	int error;
    615   1.9  jmcneill 
    616   1.9  jmcneill 	sc->sc_hwvol[dest] = hwvol;
    617   1.9  jmcneill 	if (dest != sc->sc_dest)
    618   1.9  jmcneill 		return 0;
    619   1.9  jmcneill 
    620   1.9  jmcneill 	vchi_service_use(sc->sc_service);
    621   1.9  jmcneill 
    622   1.9  jmcneill 	memset(&msg, 0, sizeof(msg));
    623   1.9  jmcneill 	msg.type = VC_AUDIO_MSG_TYPE_CONTROL;
    624   1.9  jmcneill 	msg.u.control.volume = vol2vc(hwvol);
    625   1.9  jmcneill 	msg.u.control.dest = dest;
    626   1.9  jmcneill 
    627   1.9  jmcneill 	error = vcaudio_msg_sync(sc, &msg, sizeof(msg));
    628   1.9  jmcneill 	if (error) {
    629   1.9  jmcneill 		device_printf(sc->sc_dev,
    630   1.9  jmcneill 		    "couldn't send CONTROL message (%d)\n", error);
    631   1.9  jmcneill 	}
    632   1.9  jmcneill 
    633   1.9  jmcneill 	vchi_service_release(sc->sc_service);
    634   1.9  jmcneill 
    635   1.9  jmcneill 	return error;
    636   1.9  jmcneill }
    637   1.9  jmcneill 
    638   1.9  jmcneill static int
    639   1.1  jmcneill vcaudio_set_port(void *priv, mixer_ctrl_t *mc)
    640   1.1  jmcneill {
    641   1.1  jmcneill 	struct vcaudio_softc *sc = priv;
    642   1.1  jmcneill 
    643   1.1  jmcneill 	switch (mc->dev) {
    644   1.1  jmcneill 	case VCAUDIO_OUTPUT_MASTER_VOLUME:
    645   1.1  jmcneill 	case VCAUDIO_INPUT_DAC_VOLUME:
    646   1.8  jmcneill 		sc->sc_swvol = mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
    647   1.8  jmcneill 		return 0;
    648   1.9  jmcneill 	case VCAUDIO_OUTPUT_AUTO_VOLUME:
    649   1.9  jmcneill 		return vcaudio_set_volume(sc, VCAUDIO_DEST_AUTO,
    650   1.9  jmcneill 		    mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT]);
    651   1.8  jmcneill 	case VCAUDIO_OUTPUT_HEADPHONE_VOLUME:
    652   1.9  jmcneill 		return vcaudio_set_volume(sc, VCAUDIO_DEST_HP,
    653   1.9  jmcneill 		    mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT]);
    654   1.9  jmcneill 	case VCAUDIO_OUTPUT_HDMI_VOLUME:
    655   1.9  jmcneill 		return vcaudio_set_volume(sc, VCAUDIO_DEST_HDMI,
    656   1.9  jmcneill 		    mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT]);
    657   1.8  jmcneill 	case VCAUDIO_OUTPUT_SELECT:
    658   1.8  jmcneill 		if (mc->un.ord < 0 || mc->un.ord > 2)
    659   1.8  jmcneill 			return EINVAL;
    660   1.8  jmcneill 		sc->sc_dest = mc->un.ord;
    661   1.9  jmcneill 		return vcaudio_set_volume(sc, mc->un.ord,
    662   1.9  jmcneill 		    sc->sc_hwvol[mc->un.ord]);
    663   1.1  jmcneill 	}
    664   1.1  jmcneill 	return ENXIO;
    665   1.1  jmcneill }
    666   1.1  jmcneill 
    667   1.1  jmcneill static int
    668   1.1  jmcneill vcaudio_get_port(void *priv, mixer_ctrl_t *mc)
    669   1.1  jmcneill {
    670   1.1  jmcneill 	struct vcaudio_softc *sc = priv;
    671   1.1  jmcneill 
    672   1.1  jmcneill 	switch (mc->dev) {
    673   1.1  jmcneill 	case VCAUDIO_OUTPUT_MASTER_VOLUME:
    674   1.1  jmcneill 	case VCAUDIO_INPUT_DAC_VOLUME:
    675   1.9  jmcneill 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] =
    676   1.9  jmcneill 		    mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] =
    677   1.9  jmcneill 		    sc->sc_swvol;
    678   1.9  jmcneill 		return 0;
    679   1.9  jmcneill 	case VCAUDIO_OUTPUT_AUTO_VOLUME:
    680   1.9  jmcneill 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] =
    681   1.9  jmcneill 		    mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] =
    682   1.9  jmcneill 		    sc->sc_hwvol[VCAUDIO_DEST_AUTO];
    683   1.8  jmcneill 		return 0;
    684   1.8  jmcneill 	case VCAUDIO_OUTPUT_HEADPHONE_VOLUME:
    685   1.1  jmcneill 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] =
    686   1.1  jmcneill 		    mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] =
    687   1.9  jmcneill 		    sc->sc_hwvol[VCAUDIO_DEST_HP];
    688   1.9  jmcneill 		return 0;
    689   1.9  jmcneill 	case VCAUDIO_OUTPUT_HDMI_VOLUME:
    690   1.9  jmcneill 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] =
    691   1.9  jmcneill 		    mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] =
    692   1.9  jmcneill 		    sc->sc_hwvol[VCAUDIO_DEST_HDMI];
    693   1.8  jmcneill 		return 0;
    694   1.8  jmcneill 	case VCAUDIO_OUTPUT_SELECT:
    695   1.8  jmcneill 		mc->un.ord = sc->sc_dest;
    696   1.1  jmcneill 		return 0;
    697   1.1  jmcneill 	}
    698   1.1  jmcneill 	return ENXIO;
    699   1.1  jmcneill }
    700   1.1  jmcneill 
    701   1.1  jmcneill static int
    702   1.1  jmcneill vcaudio_query_devinfo(void *priv, mixer_devinfo_t *di)
    703   1.1  jmcneill {
    704   1.1  jmcneill 	switch (di->index) {
    705   1.1  jmcneill 	case VCAUDIO_OUTPUT_CLASS:
    706   1.1  jmcneill 		di->mixer_class = VCAUDIO_OUTPUT_CLASS;
    707   1.1  jmcneill 		strcpy(di->label.name, AudioCoutputs);
    708   1.1  jmcneill 		di->type = AUDIO_MIXER_CLASS;
    709   1.1  jmcneill 		di->next = di->prev = AUDIO_MIXER_LAST;
    710   1.1  jmcneill 		return 0;
    711   1.1  jmcneill 	case VCAUDIO_INPUT_CLASS:
    712   1.1  jmcneill 		di->mixer_class = VCAUDIO_INPUT_CLASS;
    713   1.1  jmcneill 		strcpy(di->label.name, AudioCinputs);
    714   1.1  jmcneill 		di->type = AUDIO_MIXER_CLASS;
    715   1.1  jmcneill 		di->next = di->prev = AUDIO_MIXER_LAST;
    716   1.1  jmcneill 		return 0;
    717   1.1  jmcneill 	case VCAUDIO_OUTPUT_MASTER_VOLUME:
    718   1.1  jmcneill 		di->mixer_class = VCAUDIO_OUTPUT_CLASS;
    719   1.1  jmcneill 		strcpy(di->label.name, AudioNmaster);
    720   1.1  jmcneill 		di->type = AUDIO_MIXER_VALUE;
    721   1.1  jmcneill 		di->next = di->prev = AUDIO_MIXER_LAST;
    722   1.1  jmcneill 		di->un.v.num_channels = 2;
    723   1.1  jmcneill 		strcpy(di->un.v.units.name, AudioNvolume);
    724   1.1  jmcneill 		return 0;
    725   1.9  jmcneill 	case VCAUDIO_OUTPUT_AUTO_VOLUME:
    726   1.9  jmcneill 		di->mixer_class = VCAUDIO_OUTPUT_CLASS;
    727   1.9  jmcneill 		strcpy(di->label.name, "auto");
    728   1.9  jmcneill 		di->type = AUDIO_MIXER_VALUE;
    729   1.9  jmcneill 		di->next = di->prev = AUDIO_MIXER_LAST;
    730   1.9  jmcneill 		di->un.v.num_channels = 2;
    731   1.9  jmcneill 		di->un.v.delta = 13;
    732   1.9  jmcneill 		strcpy(di->un.v.units.name, AudioNvolume);
    733   1.9  jmcneill 		return 0;
    734   1.8  jmcneill 	case VCAUDIO_OUTPUT_HEADPHONE_VOLUME:
    735   1.8  jmcneill 		di->mixer_class = VCAUDIO_OUTPUT_CLASS;
    736   1.8  jmcneill 		strcpy(di->label.name, AudioNheadphone);
    737   1.8  jmcneill 		di->type = AUDIO_MIXER_VALUE;
    738   1.8  jmcneill 		di->next = di->prev = AUDIO_MIXER_LAST;
    739   1.8  jmcneill 		di->un.v.num_channels = 2;
    740   1.9  jmcneill 		di->un.v.delta = 13;
    741   1.9  jmcneill 		strcpy(di->un.v.units.name, AudioNvolume);
    742   1.9  jmcneill 		return 0;
    743   1.9  jmcneill 	case VCAUDIO_OUTPUT_HDMI_VOLUME:
    744   1.9  jmcneill 		di->mixer_class = VCAUDIO_OUTPUT_CLASS;
    745   1.9  jmcneill 		strcpy(di->label.name, "hdmi");
    746   1.9  jmcneill 		di->type = AUDIO_MIXER_VALUE;
    747   1.9  jmcneill 		di->next = di->prev = AUDIO_MIXER_LAST;
    748   1.9  jmcneill 		di->un.v.num_channels = 2;
    749   1.9  jmcneill 		di->un.v.delta = 13;
    750   1.8  jmcneill 		strcpy(di->un.v.units.name, AudioNvolume);
    751   1.8  jmcneill 		return 0;
    752   1.1  jmcneill 	case VCAUDIO_INPUT_DAC_VOLUME:
    753   1.1  jmcneill 		di->mixer_class = VCAUDIO_INPUT_CLASS;
    754   1.1  jmcneill 		strcpy(di->label.name, AudioNdac);
    755   1.1  jmcneill 		di->type = AUDIO_MIXER_VALUE;
    756   1.1  jmcneill 		di->next = di->prev = AUDIO_MIXER_LAST;
    757   1.1  jmcneill 		di->un.v.num_channels = 2;
    758   1.1  jmcneill 		strcpy(di->un.v.units.name, AudioNvolume);
    759   1.1  jmcneill 		return 0;
    760   1.8  jmcneill 	case VCAUDIO_OUTPUT_SELECT:
    761   1.8  jmcneill 		di->mixer_class = VCAUDIO_OUTPUT_CLASS;
    762   1.8  jmcneill 		strcpy(di->label.name, AudioNselect);
    763   1.8  jmcneill 		di->type = AUDIO_MIXER_ENUM;
    764   1.8  jmcneill 		di->next = di->prev = AUDIO_MIXER_LAST;
    765   1.8  jmcneill 		di->un.e.num_mem = 3;
    766   1.8  jmcneill 		di->un.e.member[0].ord = 0;
    767   1.8  jmcneill 		strcpy(di->un.e.member[0].label.name, "auto");
    768   1.8  jmcneill 		di->un.e.member[1].ord = 1;
    769   1.8  jmcneill 		strcpy(di->un.e.member[1].label.name, AudioNheadphone);
    770   1.8  jmcneill 		di->un.e.member[2].ord = 2;
    771   1.8  jmcneill 		strcpy(di->un.e.member[2].label.name, "hdmi");
    772   1.8  jmcneill 		return 0;
    773   1.1  jmcneill 	}
    774   1.1  jmcneill 
    775   1.1  jmcneill 	return ENXIO;
    776   1.1  jmcneill }
    777   1.1  jmcneill 
    778   1.1  jmcneill static int
    779   1.1  jmcneill vcaudio_getdev(void *priv, struct audio_device *audiodev)
    780   1.1  jmcneill {
    781   1.6  jmcneill 	struct vcaudio_softc *sc = priv;
    782   1.6  jmcneill 
    783   1.7     skrll 	snprintf(audiodev->name, sizeof(audiodev->name), "vchiq auds");
    784   1.6  jmcneill 	snprintf(audiodev->version, sizeof(audiodev->version),
    785   1.6  jmcneill 	    "%d", sc->sc_peer_version);
    786   1.1  jmcneill 	snprintf(audiodev->config, sizeof(audiodev->config), "vcaudio");
    787   1.6  jmcneill 
    788   1.1  jmcneill 	return 0;
    789   1.1  jmcneill }
    790   1.1  jmcneill 
    791   1.1  jmcneill static int
    792   1.1  jmcneill vcaudio_get_props(void *priv)
    793   1.1  jmcneill {
    794  1.16       nia 	return AUDIO_PROP_PLAYBACK;
    795   1.1  jmcneill }
    796   1.1  jmcneill 
    797   1.1  jmcneill static int
    798   1.1  jmcneill vcaudio_round_blocksize(void *priv, int bs, int mode,
    799   1.1  jmcneill     const audio_params_t *params)
    800   1.1  jmcneill {
    801   1.7     skrll 	return VCAUDIO_BLOCKSIZE;
    802   1.1  jmcneill }
    803   1.1  jmcneill 
    804   1.1  jmcneill static int
    805   1.1  jmcneill vcaudio_trigger_output(void *priv, void *start, void *end, int blksize,
    806   1.1  jmcneill     void (*intr)(void *), void *intrarg, const audio_params_t *params)
    807   1.1  jmcneill {
    808   1.1  jmcneill 	struct vcaudio_softc *sc = priv;
    809   1.1  jmcneill 
    810   1.7     skrll 	ASSERT_SLEEPABLE();
    811   1.7     skrll 	KASSERT(mutex_owned(&sc->sc_intr_lock));
    812   1.7     skrll 
    813   1.1  jmcneill 	sc->sc_pparam = *params;
    814   1.1  jmcneill 	sc->sc_pint = intr;
    815   1.1  jmcneill 	sc->sc_pintarg = intrarg;
    816   1.1  jmcneill 	sc->sc_ppos = 0;
    817   1.1  jmcneill 	sc->sc_pstart = start;
    818   1.1  jmcneill 	sc->sc_pend = end;
    819   1.1  jmcneill 	sc->sc_pblksize = blksize;
    820   1.7     skrll 	sc->sc_pblkcnt = 0;
    821   1.7     skrll 	sc->sc_pbytes = 0;
    822   1.7     skrll 	sc->sc_abytes = blksize;
    823   1.7     skrll 
    824   1.7     skrll 	cv_signal(&sc->sc_datacv);
    825   1.1  jmcneill 
    826   1.1  jmcneill 	return 0;
    827   1.1  jmcneill }
    828   1.2     skrll 
    829   1.1  jmcneill static void
    830   1.1  jmcneill vcaudio_get_locks(void *priv, kmutex_t **intr, kmutex_t **thread)
    831   1.1  jmcneill {
    832   1.1  jmcneill 	struct vcaudio_softc *sc = priv;
    833   1.1  jmcneill 
    834   1.1  jmcneill 	*intr = &sc->sc_intr_lock;
    835   1.1  jmcneill 	*thread = &sc->sc_lock;
    836   1.1  jmcneill }
    837   1.8  jmcneill 
    838   1.8  jmcneill static void
    839  1.14     isaki vcaudio_swvol_codec(audio_filter_arg_t *arg)
    840   1.8  jmcneill {
    841  1.14     isaki 	struct vcaudio_softc *sc = arg->context;
    842  1.14     isaki 	const aint_t *src;
    843  1.19   mlelstv 	int16_t *dst;
    844  1.14     isaki 	u_int sample_count;
    845  1.14     isaki 	u_int i;
    846  1.14     isaki 
    847  1.14     isaki 	src = arg->src;
    848  1.14     isaki 	dst = arg->dst;
    849  1.14     isaki 	sample_count = arg->count * arg->srcfmt->channels;
    850  1.14     isaki 	for (i = 0; i < sample_count; i++) {
    851  1.14     isaki 		aint2_t v = (aint2_t)(*src++);
    852  1.14     isaki 		v = v * sc->sc_swvol / 255;
    853  1.14     isaki 		*dst++ = (aint_t)v;
    854  1.14     isaki 	}
    855   1.8  jmcneill }
    856