Home | History | Annotate | Line # | Download | only in broadcom
bcm2835_vcaudio.c revision 1.11.16.1
      1  1.11.16.1  christos /* $NetBSD: bcm2835_vcaudio.c,v 1.11.16.1 2019/06/10 22:05:52 christos 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.11.16.1  christos __KERNEL_RCSID(0, "$NetBSD: bcm2835_vcaudio.c,v 1.11.16.1 2019/06/10 22:05:52 christos 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.11.16.1  christos #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.11.16.1  christos /* 40ms block of 16bit 48kHz stereo is 7680 bytes. */
     97  1.11.16.1  christos #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.11.16.1  christos /* 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.11.16.1  christos static int	vcaudio_query_format(void *, audio_format_query_t *);
    158  1.11.16.1  christos static int	vcaudio_set_format(void *, int,
    159  1.11.16.1  christos     const audio_params_t *, const audio_params_t *,
    160  1.11.16.1  christos     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_halt_input(void *);
    163        1.1  jmcneill static int	vcaudio_set_port(void *, mixer_ctrl_t *);
    164        1.1  jmcneill static int	vcaudio_get_port(void *, mixer_ctrl_t *);
    165        1.1  jmcneill static int	vcaudio_query_devinfo(void *, mixer_devinfo_t *);
    166        1.1  jmcneill static int	vcaudio_getdev(void *, struct audio_device *);
    167        1.1  jmcneill static int	vcaudio_get_props(void *);
    168        1.7     skrll 
    169        1.7     skrll static int	vcaudio_round_blocksize(void *, int, int,
    170        1.7     skrll     const audio_params_t *);
    171        1.7     skrll 
    172        1.1  jmcneill static int	vcaudio_trigger_output(void *, void *, void *, int,
    173        1.7     skrll     void (*)(void *), void *, const audio_params_t *);
    174        1.1  jmcneill static int	vcaudio_trigger_input(void *, void *, void *, int,
    175        1.7     skrll     void (*)(void *), void *, const audio_params_t *);
    176        1.7     skrll 
    177        1.1  jmcneill static void	vcaudio_get_locks(void *, kmutex_t **, kmutex_t **);
    178        1.1  jmcneill 
    179  1.11.16.1  christos static void vcaudio_swvol_codec(audio_filter_arg_t *);
    180        1.8  jmcneill 
    181        1.1  jmcneill static const struct audio_hw_if vcaudio_hw_if = {
    182  1.11.16.1  christos 	.query_format = vcaudio_query_format,
    183  1.11.16.1  christos 	.set_format = vcaudio_set_format,
    184        1.1  jmcneill 	.halt_output = vcaudio_halt_output,
    185        1.1  jmcneill 	.halt_input = vcaudio_halt_input,
    186        1.1  jmcneill 	.getdev = vcaudio_getdev,
    187        1.1  jmcneill 	.set_port = vcaudio_set_port,
    188        1.1  jmcneill 	.get_port = vcaudio_get_port,
    189        1.1  jmcneill 	.query_devinfo = vcaudio_query_devinfo,
    190        1.1  jmcneill 	.get_props = vcaudio_get_props,
    191        1.1  jmcneill 	.round_blocksize = vcaudio_round_blocksize,
    192        1.1  jmcneill 	.trigger_output = vcaudio_trigger_output,
    193        1.1  jmcneill 	.trigger_input = vcaudio_trigger_input,
    194        1.1  jmcneill 	.get_locks = vcaudio_get_locks,
    195        1.1  jmcneill };
    196        1.1  jmcneill 
    197        1.1  jmcneill CFATTACH_DECL2_NEW(vcaudio, sizeof(struct vcaudio_softc),
    198        1.7     skrll     vcaudio_match, vcaudio_attach, NULL, NULL, vcaudio_rescan,
    199        1.7     skrll     vcaudio_childdet);
    200        1.1  jmcneill 
    201        1.1  jmcneill static int
    202        1.1  jmcneill vcaudio_match(device_t parent, cfdata_t match, void *aux)
    203        1.1  jmcneill {
    204        1.1  jmcneill 	struct vchiq_attach_args *vaa = aux;
    205        1.1  jmcneill 
    206        1.1  jmcneill 	return !strcmp(vaa->vaa_name, "AUDS");
    207        1.1  jmcneill }
    208        1.1  jmcneill 
    209        1.1  jmcneill static void
    210        1.1  jmcneill vcaudio_attach(device_t parent, device_t self, void *aux)
    211        1.1  jmcneill {
    212        1.1  jmcneill 	struct vcaudio_softc *sc = device_private(self);
    213        1.1  jmcneill 	int error;
    214        1.1  jmcneill 
    215        1.1  jmcneill 	sc->sc_dev = self;
    216        1.1  jmcneill 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    217        1.3     skrll 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_NONE);
    218        1.3     skrll 	mutex_init(&sc->sc_msglock, MUTEX_DEFAULT, IPL_NONE);
    219        1.7     skrll 	cv_init(&sc->sc_msgcv, "msg");
    220        1.7     skrll 	cv_init(&sc->sc_datacv, "data");
    221        1.3     skrll 	sc->sc_success = -1;
    222        1.7     skrll 
    223        1.7     skrll 	error = kthread_create(PRI_BIO, KTHREAD_MPSAFE, NULL, vcaudio_worker,
    224        1.7     skrll 	    sc, &sc->sc_lwp, "vcaudio");
    225        1.1  jmcneill 	if (error) {
    226        1.7     skrll 		aprint_error(": couldn't create thread (%d)\n", error);
    227        1.1  jmcneill 		return;
    228        1.1  jmcneill 	}
    229        1.1  jmcneill 
    230        1.1  jmcneill 	aprint_naive("\n");
    231        1.7     skrll 	aprint_normal(": auds\n");
    232        1.7     skrll 
    233        1.7     skrll 	error = vcaudio_rescan(self, NULL, NULL);
    234        1.7     skrll 	if (error)
    235        1.7     skrll 		aprint_error_dev(self, "not configured\n");
    236        1.1  jmcneill 
    237        1.1  jmcneill }
    238        1.1  jmcneill 
    239        1.1  jmcneill static int
    240        1.1  jmcneill vcaudio_rescan(device_t self, const char *ifattr, const int *locs)
    241        1.1  jmcneill {
    242        1.1  jmcneill 	struct vcaudio_softc *sc = device_private(self);
    243        1.6  jmcneill 	int error;
    244        1.1  jmcneill 
    245        1.1  jmcneill 	if (ifattr_match(ifattr, "audiobus") && sc->sc_audiodev == NULL) {
    246        1.6  jmcneill 		error = vcaudio_init(sc);
    247        1.7     skrll 		if (error) {
    248        1.6  jmcneill 			return error;
    249        1.7     skrll 		}
    250        1.6  jmcneill 
    251        1.1  jmcneill 		sc->sc_audiodev = audio_attach_mi(&vcaudio_hw_if,
    252        1.1  jmcneill 		    sc, sc->sc_dev);
    253        1.1  jmcneill 	}
    254        1.1  jmcneill 	return 0;
    255        1.1  jmcneill }
    256        1.1  jmcneill 
    257        1.1  jmcneill static void
    258        1.1  jmcneill vcaudio_childdet(device_t self, device_t child)
    259        1.1  jmcneill {
    260        1.1  jmcneill 	struct vcaudio_softc *sc = device_private(self);
    261        1.1  jmcneill 
    262        1.1  jmcneill 	if (sc->sc_audiodev == child)
    263        1.1  jmcneill 		sc->sc_audiodev = NULL;
    264        1.1  jmcneill }
    265        1.1  jmcneill 
    266        1.1  jmcneill static int
    267        1.1  jmcneill vcaudio_init(struct vcaudio_softc *sc)
    268        1.1  jmcneill {
    269        1.1  jmcneill 	VC_AUDIO_MSG_T msg;
    270        1.1  jmcneill 	int error;
    271        1.1  jmcneill 
    272        1.8  jmcneill 	sc->sc_swvol = 255;
    273        1.9  jmcneill 	sc->sc_hwvol[VCAUDIO_DEST_AUTO] = 255;
    274        1.9  jmcneill 	sc->sc_hwvol[VCAUDIO_DEST_HP] = 255;
    275        1.9  jmcneill 	sc->sc_hwvol[VCAUDIO_DEST_HDMI] = 255;
    276        1.1  jmcneill 	sc->sc_dest = VCAUDIO_DEST_AUTO;
    277        1.1  jmcneill 
    278        1.1  jmcneill 	sc->sc_format.mode = AUMODE_PLAY|AUMODE_RECORD;
    279        1.1  jmcneill 	sc->sc_format.encoding = AUDIO_ENCODING_SLINEAR_LE;
    280        1.1  jmcneill 	sc->sc_format.validbits = 16;
    281        1.1  jmcneill 	sc->sc_format.precision = 16;
    282        1.1  jmcneill 	sc->sc_format.channels = 2;
    283        1.1  jmcneill 	sc->sc_format.channel_mask = AUFMT_STEREO;
    284        1.1  jmcneill 	sc->sc_format.frequency_type = 0;
    285        1.4  jmcneill 	sc->sc_format.frequency[0] = 48000;
    286        1.1  jmcneill 	sc->sc_format.frequency[1] = 48000;
    287        1.2     skrll 
    288        1.1  jmcneill 	error = vchi_initialise(&sc->sc_instance);
    289        1.1  jmcneill 	if (error) {
    290        1.7     skrll 		aprint_error_dev(sc->sc_dev,
    291        1.7     skrll 		    "couldn't init vchi instance (%d)\n", error);
    292        1.1  jmcneill 		return EIO;
    293        1.1  jmcneill 	}
    294        1.1  jmcneill 
    295        1.1  jmcneill 	error = vchi_connect(NULL, 0, sc->sc_instance);
    296        1.1  jmcneill 	if (error) {
    297        1.7     skrll 		aprint_error_dev(sc->sc_dev,
    298        1.7     skrll 		    "couldn't connect vchi (%d)\n", error);
    299        1.1  jmcneill 		return EIO;
    300        1.1  jmcneill 	}
    301        1.1  jmcneill 
    302        1.1  jmcneill 	SERVICE_CREATION_T setup = {
    303        1.1  jmcneill 		.version = VCHI_VERSION(VC_AUDIOSERV_VER),
    304        1.1  jmcneill 		.service_id = VC_AUDIO_SERVER_NAME,
    305        1.1  jmcneill 		.connection = &sc->sc_connection,
    306        1.1  jmcneill 		.rx_fifo_size = 0,
    307        1.1  jmcneill 		.tx_fifo_size = 0,
    308        1.1  jmcneill 		.callback = vcaudio_service_callback,
    309        1.1  jmcneill 		.callback_param = sc,
    310        1.1  jmcneill 		.want_unaligned_bulk_rx = 1,
    311        1.1  jmcneill 		.want_unaligned_bulk_tx = 1,
    312        1.1  jmcneill 		.want_crc = 0,
    313        1.1  jmcneill 	};
    314        1.1  jmcneill 
    315        1.1  jmcneill 	error = vchi_service_open(sc->sc_instance, &setup, &sc->sc_service);
    316        1.1  jmcneill 	if (error) {
    317        1.7     skrll 		aprint_error_dev(sc->sc_dev, "couldn't open service (%d)\n",
    318        1.1  jmcneill 		    error);
    319        1.1  jmcneill 		return EIO;
    320        1.1  jmcneill 	}
    321        1.1  jmcneill 
    322        1.6  jmcneill 	vchi_get_peer_version(sc->sc_service, &sc->sc_peer_version);
    323        1.6  jmcneill 
    324        1.6  jmcneill 	if (sc->sc_peer_version < 2) {
    325        1.6  jmcneill 		aprint_error_dev(sc->sc_dev,
    326        1.6  jmcneill 		    "peer version (%d) is less than the required version (2)\n",
    327        1.6  jmcneill 		    sc->sc_peer_version);
    328        1.6  jmcneill 		return EINVAL;
    329        1.6  jmcneill 	}
    330        1.6  jmcneill 
    331        1.1  jmcneill 	memset(&msg, 0, sizeof(msg));
    332        1.1  jmcneill 	msg.type = VC_AUDIO_MSG_TYPE_OPEN;
    333        1.1  jmcneill 	error = vchi_msg_queue(sc->sc_service, &msg, sizeof(msg),
    334        1.1  jmcneill 	    VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
    335        1.1  jmcneill 	if (error) {
    336        1.7     skrll 		aprint_error_dev(sc->sc_dev,
    337        1.7     skrll 		    "couldn't send OPEN message (%d)\n", error);
    338        1.1  jmcneill 	}
    339        1.1  jmcneill 
    340        1.1  jmcneill 	memset(&msg, 0, sizeof(msg));
    341        1.4  jmcneill 	msg.type = VC_AUDIO_MSG_TYPE_CONFIG;
    342        1.4  jmcneill 	msg.u.config.channels = 2;
    343        1.4  jmcneill 	msg.u.config.samplerate = 48000;
    344        1.4  jmcneill 	msg.u.config.bps = 16;
    345        1.4  jmcneill 	error = vcaudio_msg_sync(sc, &msg, sizeof(msg));
    346        1.4  jmcneill 	if (error) {
    347        1.7     skrll 		aprint_error_dev(sc->sc_dev,
    348        1.7     skrll 		    "couldn't send CONFIG message (%d)\n", error);
    349        1.4  jmcneill 	}
    350        1.4  jmcneill 
    351        1.4  jmcneill 	memset(&msg, 0, sizeof(msg));
    352        1.1  jmcneill 	msg.type = VC_AUDIO_MSG_TYPE_CONTROL;
    353        1.9  jmcneill 	msg.u.control.volume = vol2vc(sc->sc_hwvol[sc->sc_dest]);
    354        1.8  jmcneill 	msg.u.control.dest = sc->sc_dest;
    355        1.1  jmcneill 	error = vcaudio_msg_sync(sc, &msg, sizeof(msg));
    356        1.1  jmcneill 	if (error) {
    357        1.7     skrll 		aprint_error_dev(sc->sc_dev,
    358        1.7     skrll 		    "couldn't send CONTROL message (%d)\n", error);
    359        1.1  jmcneill 	}
    360        1.7     skrll 
    361        1.1  jmcneill 	vchi_service_release(sc->sc_service);
    362        1.1  jmcneill 
    363        1.1  jmcneill 	return 0;
    364        1.1  jmcneill }
    365        1.1  jmcneill 
    366        1.1  jmcneill static void
    367        1.1  jmcneill vcaudio_service_callback(void *priv, const VCHI_CALLBACK_REASON_T reason,
    368        1.1  jmcneill     void *msg_handle)
    369        1.1  jmcneill {
    370        1.1  jmcneill 	struct vcaudio_softc *sc = priv;
    371        1.1  jmcneill 	VC_AUDIO_MSG_T msg;
    372        1.1  jmcneill 	int32_t msglen = 0;
    373        1.1  jmcneill 	int error;
    374        1.1  jmcneill 	void (*intr)(void *) = NULL;
    375        1.1  jmcneill 	void *intrarg = NULL;
    376        1.1  jmcneill 
    377        1.1  jmcneill 	if (sc == NULL || reason != VCHI_CALLBACK_MSG_AVAILABLE)
    378        1.1  jmcneill 		return;
    379        1.1  jmcneill 
    380        1.1  jmcneill 	memset(&msg, 0, sizeof(msg));
    381        1.1  jmcneill 	error = vchi_msg_dequeue(sc->sc_service, &msg, sizeof(msg), &msglen,
    382        1.1  jmcneill 	    VCHI_FLAGS_NONE);
    383        1.1  jmcneill 	if (error) {
    384        1.1  jmcneill 		device_printf(sc->sc_dev, "couldn't dequeue msg (%d)\n",
    385        1.1  jmcneill 		    error);
    386        1.1  jmcneill 		return;
    387        1.1  jmcneill 	}
    388        1.1  jmcneill 
    389        1.1  jmcneill 	switch (msg.type) {
    390        1.1  jmcneill 	case VC_AUDIO_MSG_TYPE_RESULT:
    391        1.3     skrll 		mutex_enter(&sc->sc_msglock);
    392        1.1  jmcneill 		sc->sc_success = msg.u.result.success;
    393        1.3     skrll 		sc->sc_msgdone = true;
    394        1.3     skrll 		cv_broadcast(&sc->sc_msgcv);
    395        1.3     skrll 		mutex_exit(&sc->sc_msglock);
    396        1.1  jmcneill 		break;
    397        1.7     skrll 
    398        1.1  jmcneill 	case VC_AUDIO_MSG_TYPE_COMPLETE:
    399        1.1  jmcneill 		intr = msg.u.complete.callback;
    400        1.1  jmcneill 		intrarg = msg.u.complete.cookie;
    401        1.1  jmcneill 		if (intr && intrarg) {
    402        1.5  jmcneill 			int count = msg.u.complete.count & 0xffff;
    403        1.7     skrll 			int perr = (msg.u.complete.count & __BIT(30)) != 0;
    404        1.5  jmcneill 			bool sched = false;
    405        1.7     skrll 
    406        1.3     skrll 			mutex_enter(&sc->sc_intr_lock);
    407        1.7     skrll 
    408        1.5  jmcneill 			if (count > 0) {
    409        1.5  jmcneill 				sc->sc_pbytes += count;
    410        1.5  jmcneill 			}
    411        1.5  jmcneill 			if (perr && sc->sc_started) {
    412        1.5  jmcneill #ifdef VCAUDIO_DEBUG
    413        1.5  jmcneill 				device_printf(sc->sc_dev, "underrun\n");
    414        1.5  jmcneill #endif
    415        1.5  jmcneill 				sched = true;
    416        1.1  jmcneill 			}
    417        1.1  jmcneill 			if (sc->sc_pbytes >= sc->sc_pblksize) {
    418        1.1  jmcneill 				sc->sc_pbytes -= sc->sc_pblksize;
    419        1.5  jmcneill 				sched = true;
    420        1.5  jmcneill 			}
    421        1.5  jmcneill 
    422       1.11       nat 			if (sched && sc->sc_pint) {
    423        1.1  jmcneill 				intr(intrarg);
    424        1.7     skrll 				sc->sc_abytes += sc->sc_pblksize;
    425        1.7     skrll 				cv_signal(&sc->sc_datacv);
    426        1.1  jmcneill 			}
    427        1.3     skrll 			mutex_exit(&sc->sc_intr_lock);
    428        1.1  jmcneill 		}
    429        1.1  jmcneill 		break;
    430        1.1  jmcneill 	default:
    431        1.1  jmcneill 		break;
    432        1.1  jmcneill 	}
    433        1.1  jmcneill }
    434        1.1  jmcneill 
    435        1.1  jmcneill static void
    436        1.7     skrll vcaudio_worker(void *priv)
    437        1.1  jmcneill {
    438        1.1  jmcneill 	struct vcaudio_softc *sc = priv;
    439        1.1  jmcneill 	VC_AUDIO_MSG_T msg;
    440        1.1  jmcneill 	void (*intr)(void *);
    441        1.1  jmcneill 	void *intrarg;
    442        1.1  jmcneill 	void *block;
    443        1.1  jmcneill 	int error, resid, off, nb, count;
    444        1.1  jmcneill 
    445        1.1  jmcneill 	mutex_enter(&sc->sc_intr_lock);
    446        1.1  jmcneill 
    447        1.7     skrll 	while (true) {
    448        1.7     skrll 		intr = sc->sc_pint;
    449        1.7     skrll 		intrarg = sc->sc_pintarg;
    450        1.7     skrll 
    451        1.7     skrll 		if (intr == NULL || intrarg == NULL) {
    452        1.7     skrll 			cv_wait_sig(&sc->sc_datacv, &sc->sc_intr_lock);
    453        1.7     skrll 			continue;
    454        1.7     skrll 		}
    455        1.1  jmcneill 
    456        1.7     skrll 		KASSERT(sc->sc_pblksize != 0);
    457        1.1  jmcneill 
    458        1.7     skrll 		if (sc->sc_abytes < sc->sc_pblksize) {
    459        1.7     skrll 			cv_wait_sig(&sc->sc_datacv, &sc->sc_intr_lock);
    460        1.7     skrll 			continue;
    461        1.7     skrll 		}
    462        1.7     skrll 		count = sc->sc_pblksize;
    463        1.1  jmcneill 
    464        1.1  jmcneill 		memset(&msg, 0, sizeof(msg));
    465        1.7     skrll 		msg.type = VC_AUDIO_MSG_TYPE_WRITE;
    466        1.7     skrll 		msg.u.write.max_packet = VCAUDIO_MSGSIZE;
    467        1.7     skrll 		msg.u.write.count = count;
    468        1.7     skrll 		msg.u.write.callback = intr;
    469        1.7     skrll 		msg.u.write.cookie = intrarg;
    470        1.7     skrll 		msg.u.write.silence = 0;
    471        1.7     skrll 
    472  1.11.16.1  christos 		block = (uint8_t *)sc->sc_pstart + sc->sc_ppos;
    473        1.7     skrll 		resid = count;
    474        1.7     skrll 		off = 0;
    475        1.7     skrll 
    476        1.7     skrll 		vchi_service_use(sc->sc_service);
    477        1.7     skrll 
    478        1.1  jmcneill 		error = vchi_msg_queue(sc->sc_service, &msg, sizeof(msg),
    479        1.1  jmcneill 		    VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
    480        1.1  jmcneill 		if (error) {
    481        1.7     skrll 			printf("%s: failed to write (%d)\n", __func__, error);
    482        1.1  jmcneill 			goto done;
    483        1.1  jmcneill 		}
    484        1.1  jmcneill 
    485        1.7     skrll 		while (resid > 0) {
    486  1.11.16.1  christos 			nb = uimin(resid, msg.u.write.max_packet);
    487        1.7     skrll 			error = vchi_msg_queue(sc->sc_service,
    488        1.7     skrll 			    (char *)block + off, nb,
    489        1.7     skrll 			    VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
    490        1.7     skrll 			if (error) {
    491        1.7     skrll 				/* XXX What to do here? */
    492        1.7     skrll 				device_printf(sc->sc_dev,
    493        1.7     skrll 				    "failed to queue data (%d)\n", error);
    494        1.7     skrll 				goto done;
    495        1.7     skrll 			}
    496        1.7     skrll 			off += nb;
    497        1.7     skrll 			resid -= nb;
    498        1.7     skrll 		}
    499        1.1  jmcneill 
    500        1.7     skrll 		sc->sc_abytes -= count;
    501        1.7     skrll 		sc->sc_ppos += count;
    502        1.7     skrll 		if ((uint8_t *)sc->sc_pstart + sc->sc_ppos >=
    503        1.7     skrll 		    (uint8_t *)sc->sc_pend)
    504        1.7     skrll 			sc->sc_ppos = 0;
    505        1.7     skrll 
    506        1.7     skrll 		if (!sc->sc_started) {
    507  1.11.16.1  christos 			++sc->sc_pblkcnt;
    508        1.7     skrll 
    509        1.7     skrll 			if (sc->sc_pblkcnt == VCAUDIO_PREFILLCOUNT) {
    510        1.7     skrll 
    511        1.7     skrll 				memset(&msg, 0, sizeof(msg));
    512        1.7     skrll 				msg.type = VC_AUDIO_MSG_TYPE_START;
    513        1.7     skrll 				error = vchi_msg_queue(sc->sc_service, &msg,
    514        1.7     skrll 				    sizeof(msg), VCHI_FLAGS_BLOCK_UNTIL_QUEUED,
    515        1.7     skrll 				    NULL);
    516        1.7     skrll 				if (error) {
    517        1.7     skrll 					device_printf(sc->sc_dev,
    518        1.7     skrll 					    "failed to start (%d)\n", error);
    519        1.7     skrll 					goto done;
    520        1.7     skrll 				}
    521        1.7     skrll 				sc->sc_started = true;
    522        1.7     skrll 				sc->sc_pbytes = 0;
    523        1.7     skrll 			} else {
    524        1.7     skrll 				intr(intrarg);
    525        1.7     skrll 				sc->sc_abytes += sc->sc_pblksize;
    526        1.7     skrll 			}
    527        1.1  jmcneill 		}
    528        1.1  jmcneill 
    529        1.1  jmcneill done:
    530        1.7     skrll 		vchi_service_release(sc->sc_service);
    531        1.7     skrll 	}
    532        1.1  jmcneill }
    533        1.1  jmcneill 
    534        1.1  jmcneill static int
    535        1.1  jmcneill vcaudio_msg_sync(struct vcaudio_softc *sc, VC_AUDIO_MSG_T *msg, size_t msglen)
    536        1.1  jmcneill {
    537        1.1  jmcneill 	int error = 0;
    538        1.1  jmcneill 
    539        1.3     skrll 	mutex_enter(&sc->sc_msglock);
    540        1.3     skrll 
    541        1.1  jmcneill 	sc->sc_success = -1;
    542        1.3     skrll 	sc->sc_msgdone = false;
    543        1.3     skrll 
    544        1.1  jmcneill 	error = vchi_msg_queue(sc->sc_service, msg, msglen,
    545        1.1  jmcneill 	    VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
    546        1.1  jmcneill 	if (error) {
    547        1.1  jmcneill 		printf("%s: failed to queue message (%d)\n", __func__, error);
    548        1.1  jmcneill 		goto done;
    549        1.1  jmcneill 	}
    550        1.3     skrll 
    551        1.3     skrll 	while (!sc->sc_msgdone) {
    552        1.3     skrll 		error = cv_wait_sig(&sc->sc_msgcv, &sc->sc_msglock);
    553        1.1  jmcneill 		if (error)
    554        1.1  jmcneill 			break;
    555        1.1  jmcneill 	}
    556        1.3     skrll 	if (sc->sc_success != 0)
    557        1.1  jmcneill 		error = EIO;
    558        1.1  jmcneill done:
    559        1.3     skrll 	mutex_exit(&sc->sc_msglock);
    560        1.1  jmcneill 
    561        1.1  jmcneill 	return error;
    562        1.1  jmcneill }
    563        1.1  jmcneill 
    564        1.1  jmcneill static int
    565  1.11.16.1  christos vcaudio_query_format(void *priv, audio_format_query_t *afp)
    566        1.1  jmcneill {
    567        1.1  jmcneill 	struct vcaudio_softc *sc = priv;
    568        1.1  jmcneill 
    569  1.11.16.1  christos 	return audio_query_format(&sc->sc_format, 1, afp);
    570        1.1  jmcneill }
    571        1.1  jmcneill 
    572        1.1  jmcneill static int
    573  1.11.16.1  christos vcaudio_set_format(void *priv, int setmode,
    574  1.11.16.1  christos     const audio_params_t *play, const audio_params_t *rec,
    575  1.11.16.1  christos     audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
    576        1.1  jmcneill {
    577        1.1  jmcneill 	struct vcaudio_softc *sc = priv;
    578        1.1  jmcneill 
    579  1.11.16.1  christos 	pfil->codec = vcaudio_swvol_codec;
    580  1.11.16.1  christos 	pfil->context = sc;
    581        1.1  jmcneill 
    582        1.1  jmcneill 	return 0;
    583        1.1  jmcneill }
    584        1.1  jmcneill 
    585        1.1  jmcneill static int
    586        1.1  jmcneill vcaudio_halt_output(void *priv)
    587        1.1  jmcneill {
    588        1.1  jmcneill 	struct vcaudio_softc *sc = priv;
    589        1.1  jmcneill 	VC_AUDIO_MSG_T msg;
    590        1.1  jmcneill 	int error = 0;
    591        1.1  jmcneill 
    592        1.7     skrll 	KASSERT(mutex_owned(&sc->sc_intr_lock));
    593        1.7     skrll 
    594        1.1  jmcneill 	vchi_service_use(sc->sc_service);
    595        1.1  jmcneill 	memset(&msg, 0, sizeof(msg));
    596        1.1  jmcneill 	msg.type = VC_AUDIO_MSG_TYPE_STOP;
    597        1.7     skrll 	msg.u.stop.draining = 0;
    598        1.1  jmcneill 	error = vchi_msg_queue(sc->sc_service, &msg, sizeof(msg),
    599        1.1  jmcneill 	    VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
    600        1.1  jmcneill 	if (error) {
    601        1.1  jmcneill 		device_printf(sc->sc_dev, "couldn't send STOP message (%d)\n",
    602        1.1  jmcneill 		    error);
    603        1.1  jmcneill 	}
    604        1.1  jmcneill 	vchi_service_release(sc->sc_service);
    605        1.1  jmcneill 
    606        1.1  jmcneill 	sc->sc_pint = NULL;
    607        1.1  jmcneill 	sc->sc_pintarg = NULL;
    608        1.1  jmcneill 	sc->sc_started = false;
    609        1.1  jmcneill 
    610        1.1  jmcneill #ifdef VCAUDIO_DEBUG
    611        1.1  jmcneill 	device_printf(sc->sc_dev, "halting output\n");
    612        1.1  jmcneill #endif
    613        1.1  jmcneill 
    614        1.1  jmcneill 	return error;
    615        1.1  jmcneill }
    616        1.1  jmcneill 
    617        1.1  jmcneill static int
    618        1.1  jmcneill vcaudio_halt_input(void *priv)
    619        1.1  jmcneill {
    620        1.1  jmcneill 	return EINVAL;
    621        1.1  jmcneill }
    622        1.1  jmcneill 
    623        1.1  jmcneill static int
    624        1.9  jmcneill vcaudio_set_volume(struct vcaudio_softc *sc, enum vcaudio_dest dest,
    625        1.9  jmcneill     int hwvol)
    626        1.9  jmcneill {
    627        1.9  jmcneill 	VC_AUDIO_MSG_T msg;
    628        1.9  jmcneill 	int error;
    629        1.9  jmcneill 
    630        1.9  jmcneill 	sc->sc_hwvol[dest] = hwvol;
    631        1.9  jmcneill 	if (dest != sc->sc_dest)
    632        1.9  jmcneill 		return 0;
    633        1.9  jmcneill 
    634        1.9  jmcneill 	vchi_service_use(sc->sc_service);
    635        1.9  jmcneill 
    636        1.9  jmcneill 	memset(&msg, 0, sizeof(msg));
    637        1.9  jmcneill 	msg.type = VC_AUDIO_MSG_TYPE_CONTROL;
    638        1.9  jmcneill 	msg.u.control.volume = vol2vc(hwvol);
    639        1.9  jmcneill 	msg.u.control.dest = dest;
    640        1.9  jmcneill 
    641        1.9  jmcneill 	error = vcaudio_msg_sync(sc, &msg, sizeof(msg));
    642        1.9  jmcneill 	if (error) {
    643        1.9  jmcneill 		device_printf(sc->sc_dev,
    644        1.9  jmcneill 		    "couldn't send CONTROL message (%d)\n", error);
    645        1.9  jmcneill 	}
    646        1.9  jmcneill 
    647        1.9  jmcneill 	vchi_service_release(sc->sc_service);
    648        1.9  jmcneill 
    649        1.9  jmcneill 	return error;
    650        1.9  jmcneill }
    651        1.9  jmcneill 
    652        1.9  jmcneill static int
    653        1.1  jmcneill vcaudio_set_port(void *priv, mixer_ctrl_t *mc)
    654        1.1  jmcneill {
    655        1.1  jmcneill 	struct vcaudio_softc *sc = priv;
    656        1.1  jmcneill 
    657        1.1  jmcneill 	switch (mc->dev) {
    658        1.1  jmcneill 	case VCAUDIO_OUTPUT_MASTER_VOLUME:
    659        1.1  jmcneill 	case VCAUDIO_INPUT_DAC_VOLUME:
    660        1.8  jmcneill 		sc->sc_swvol = mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
    661        1.8  jmcneill 		return 0;
    662        1.9  jmcneill 	case VCAUDIO_OUTPUT_AUTO_VOLUME:
    663        1.9  jmcneill 		return vcaudio_set_volume(sc, VCAUDIO_DEST_AUTO,
    664        1.9  jmcneill 		    mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT]);
    665        1.8  jmcneill 	case VCAUDIO_OUTPUT_HEADPHONE_VOLUME:
    666        1.9  jmcneill 		return vcaudio_set_volume(sc, VCAUDIO_DEST_HP,
    667        1.9  jmcneill 		    mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT]);
    668        1.9  jmcneill 	case VCAUDIO_OUTPUT_HDMI_VOLUME:
    669        1.9  jmcneill 		return vcaudio_set_volume(sc, VCAUDIO_DEST_HDMI,
    670        1.9  jmcneill 		    mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT]);
    671        1.8  jmcneill 	case VCAUDIO_OUTPUT_SELECT:
    672        1.8  jmcneill 		if (mc->un.ord < 0 || mc->un.ord > 2)
    673        1.8  jmcneill 			return EINVAL;
    674        1.8  jmcneill 		sc->sc_dest = mc->un.ord;
    675        1.9  jmcneill 		return vcaudio_set_volume(sc, mc->un.ord,
    676        1.9  jmcneill 		    sc->sc_hwvol[mc->un.ord]);
    677        1.1  jmcneill 	}
    678        1.1  jmcneill 	return ENXIO;
    679        1.1  jmcneill }
    680        1.1  jmcneill 
    681        1.1  jmcneill static int
    682        1.1  jmcneill vcaudio_get_port(void *priv, mixer_ctrl_t *mc)
    683        1.1  jmcneill {
    684        1.1  jmcneill 	struct vcaudio_softc *sc = priv;
    685        1.1  jmcneill 
    686        1.1  jmcneill 	switch (mc->dev) {
    687        1.1  jmcneill 	case VCAUDIO_OUTPUT_MASTER_VOLUME:
    688        1.1  jmcneill 	case VCAUDIO_INPUT_DAC_VOLUME:
    689        1.9  jmcneill 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] =
    690        1.9  jmcneill 		    mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] =
    691        1.9  jmcneill 		    sc->sc_swvol;
    692        1.9  jmcneill 		return 0;
    693        1.9  jmcneill 	case VCAUDIO_OUTPUT_AUTO_VOLUME:
    694        1.9  jmcneill 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] =
    695        1.9  jmcneill 		    mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] =
    696        1.9  jmcneill 		    sc->sc_hwvol[VCAUDIO_DEST_AUTO];
    697        1.8  jmcneill 		return 0;
    698        1.8  jmcneill 	case VCAUDIO_OUTPUT_HEADPHONE_VOLUME:
    699        1.1  jmcneill 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] =
    700        1.1  jmcneill 		    mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] =
    701        1.9  jmcneill 		    sc->sc_hwvol[VCAUDIO_DEST_HP];
    702        1.9  jmcneill 		return 0;
    703        1.9  jmcneill 	case VCAUDIO_OUTPUT_HDMI_VOLUME:
    704        1.9  jmcneill 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] =
    705        1.9  jmcneill 		    mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] =
    706        1.9  jmcneill 		    sc->sc_hwvol[VCAUDIO_DEST_HDMI];
    707        1.8  jmcneill 		return 0;
    708        1.8  jmcneill 	case VCAUDIO_OUTPUT_SELECT:
    709        1.8  jmcneill 		mc->un.ord = sc->sc_dest;
    710        1.1  jmcneill 		return 0;
    711        1.1  jmcneill 	}
    712        1.1  jmcneill 	return ENXIO;
    713        1.1  jmcneill }
    714        1.1  jmcneill 
    715        1.1  jmcneill static int
    716        1.1  jmcneill vcaudio_query_devinfo(void *priv, mixer_devinfo_t *di)
    717        1.1  jmcneill {
    718        1.1  jmcneill 	switch (di->index) {
    719        1.1  jmcneill 	case VCAUDIO_OUTPUT_CLASS:
    720        1.1  jmcneill 		di->mixer_class = VCAUDIO_OUTPUT_CLASS;
    721        1.1  jmcneill 		strcpy(di->label.name, AudioCoutputs);
    722        1.1  jmcneill 		di->type = AUDIO_MIXER_CLASS;
    723        1.1  jmcneill 		di->next = di->prev = AUDIO_MIXER_LAST;
    724        1.1  jmcneill 		return 0;
    725        1.1  jmcneill 	case VCAUDIO_INPUT_CLASS:
    726        1.1  jmcneill 		di->mixer_class = VCAUDIO_INPUT_CLASS;
    727        1.1  jmcneill 		strcpy(di->label.name, AudioCinputs);
    728        1.1  jmcneill 		di->type = AUDIO_MIXER_CLASS;
    729        1.1  jmcneill 		di->next = di->prev = AUDIO_MIXER_LAST;
    730        1.1  jmcneill 		return 0;
    731        1.1  jmcneill 	case VCAUDIO_OUTPUT_MASTER_VOLUME:
    732        1.1  jmcneill 		di->mixer_class = VCAUDIO_OUTPUT_CLASS;
    733        1.1  jmcneill 		strcpy(di->label.name, AudioNmaster);
    734        1.1  jmcneill 		di->type = AUDIO_MIXER_VALUE;
    735        1.1  jmcneill 		di->next = di->prev = AUDIO_MIXER_LAST;
    736        1.1  jmcneill 		di->un.v.num_channels = 2;
    737        1.1  jmcneill 		strcpy(di->un.v.units.name, AudioNvolume);
    738        1.1  jmcneill 		return 0;
    739        1.9  jmcneill 	case VCAUDIO_OUTPUT_AUTO_VOLUME:
    740        1.9  jmcneill 		di->mixer_class = VCAUDIO_OUTPUT_CLASS;
    741        1.9  jmcneill 		strcpy(di->label.name, "auto");
    742        1.9  jmcneill 		di->type = AUDIO_MIXER_VALUE;
    743        1.9  jmcneill 		di->next = di->prev = AUDIO_MIXER_LAST;
    744        1.9  jmcneill 		di->un.v.num_channels = 2;
    745        1.9  jmcneill 		di->un.v.delta = 13;
    746        1.9  jmcneill 		strcpy(di->un.v.units.name, AudioNvolume);
    747        1.9  jmcneill 		return 0;
    748        1.8  jmcneill 	case VCAUDIO_OUTPUT_HEADPHONE_VOLUME:
    749        1.8  jmcneill 		di->mixer_class = VCAUDIO_OUTPUT_CLASS;
    750        1.8  jmcneill 		strcpy(di->label.name, AudioNheadphone);
    751        1.8  jmcneill 		di->type = AUDIO_MIXER_VALUE;
    752        1.8  jmcneill 		di->next = di->prev = AUDIO_MIXER_LAST;
    753        1.8  jmcneill 		di->un.v.num_channels = 2;
    754        1.9  jmcneill 		di->un.v.delta = 13;
    755        1.9  jmcneill 		strcpy(di->un.v.units.name, AudioNvolume);
    756        1.9  jmcneill 		return 0;
    757        1.9  jmcneill 	case VCAUDIO_OUTPUT_HDMI_VOLUME:
    758        1.9  jmcneill 		di->mixer_class = VCAUDIO_OUTPUT_CLASS;
    759        1.9  jmcneill 		strcpy(di->label.name, "hdmi");
    760        1.9  jmcneill 		di->type = AUDIO_MIXER_VALUE;
    761        1.9  jmcneill 		di->next = di->prev = AUDIO_MIXER_LAST;
    762        1.9  jmcneill 		di->un.v.num_channels = 2;
    763        1.9  jmcneill 		di->un.v.delta = 13;
    764        1.8  jmcneill 		strcpy(di->un.v.units.name, AudioNvolume);
    765        1.8  jmcneill 		return 0;
    766        1.1  jmcneill 	case VCAUDIO_INPUT_DAC_VOLUME:
    767        1.1  jmcneill 		di->mixer_class = VCAUDIO_INPUT_CLASS;
    768        1.1  jmcneill 		strcpy(di->label.name, AudioNdac);
    769        1.1  jmcneill 		di->type = AUDIO_MIXER_VALUE;
    770        1.1  jmcneill 		di->next = di->prev = AUDIO_MIXER_LAST;
    771        1.1  jmcneill 		di->un.v.num_channels = 2;
    772        1.1  jmcneill 		strcpy(di->un.v.units.name, AudioNvolume);
    773        1.1  jmcneill 		return 0;
    774        1.8  jmcneill 	case VCAUDIO_OUTPUT_SELECT:
    775        1.8  jmcneill 		di->mixer_class = VCAUDIO_OUTPUT_CLASS;
    776        1.8  jmcneill 		strcpy(di->label.name, AudioNselect);
    777        1.8  jmcneill 		di->type = AUDIO_MIXER_ENUM;
    778        1.8  jmcneill 		di->next = di->prev = AUDIO_MIXER_LAST;
    779        1.8  jmcneill 		di->un.e.num_mem = 3;
    780        1.8  jmcneill 		di->un.e.member[0].ord = 0;
    781        1.8  jmcneill 		strcpy(di->un.e.member[0].label.name, "auto");
    782        1.8  jmcneill 		di->un.e.member[1].ord = 1;
    783        1.8  jmcneill 		strcpy(di->un.e.member[1].label.name, AudioNheadphone);
    784        1.8  jmcneill 		di->un.e.member[2].ord = 2;
    785        1.8  jmcneill 		strcpy(di->un.e.member[2].label.name, "hdmi");
    786        1.8  jmcneill 		return 0;
    787        1.1  jmcneill 	}
    788        1.1  jmcneill 
    789        1.1  jmcneill 	return ENXIO;
    790        1.1  jmcneill }
    791        1.1  jmcneill 
    792        1.1  jmcneill static int
    793        1.1  jmcneill vcaudio_getdev(void *priv, struct audio_device *audiodev)
    794        1.1  jmcneill {
    795        1.6  jmcneill 	struct vcaudio_softc *sc = priv;
    796        1.6  jmcneill 
    797        1.7     skrll 	snprintf(audiodev->name, sizeof(audiodev->name), "vchiq auds");
    798        1.6  jmcneill 	snprintf(audiodev->version, sizeof(audiodev->version),
    799        1.6  jmcneill 	    "%d", sc->sc_peer_version);
    800        1.1  jmcneill 	snprintf(audiodev->config, sizeof(audiodev->config), "vcaudio");
    801        1.6  jmcneill 
    802        1.1  jmcneill 	return 0;
    803        1.1  jmcneill }
    804        1.1  jmcneill 
    805        1.1  jmcneill static int
    806        1.1  jmcneill vcaudio_get_props(void *priv)
    807        1.1  jmcneill {
    808        1.1  jmcneill 	return AUDIO_PROP_PLAYBACK|AUDIO_PROP_CAPTURE|AUDIO_PROP_INDEPENDENT;
    809        1.1  jmcneill }
    810        1.1  jmcneill 
    811        1.1  jmcneill static int
    812        1.1  jmcneill vcaudio_round_blocksize(void *priv, int bs, int mode,
    813        1.1  jmcneill     const audio_params_t *params)
    814        1.1  jmcneill {
    815        1.7     skrll 	return VCAUDIO_BLOCKSIZE;
    816        1.1  jmcneill }
    817        1.1  jmcneill 
    818        1.1  jmcneill static int
    819        1.1  jmcneill vcaudio_trigger_output(void *priv, void *start, void *end, int blksize,
    820        1.1  jmcneill     void (*intr)(void *), void *intrarg, const audio_params_t *params)
    821        1.1  jmcneill {
    822        1.1  jmcneill 	struct vcaudio_softc *sc = priv;
    823        1.1  jmcneill 
    824        1.7     skrll 	ASSERT_SLEEPABLE();
    825        1.7     skrll 	KASSERT(mutex_owned(&sc->sc_intr_lock));
    826        1.7     skrll 
    827        1.1  jmcneill 	sc->sc_pparam = *params;
    828        1.1  jmcneill 	sc->sc_pint = intr;
    829        1.1  jmcneill 	sc->sc_pintarg = intrarg;
    830        1.1  jmcneill 	sc->sc_ppos = 0;
    831        1.1  jmcneill 	sc->sc_pstart = start;
    832        1.1  jmcneill 	sc->sc_pend = end;
    833        1.1  jmcneill 	sc->sc_pblksize = blksize;
    834        1.7     skrll 	sc->sc_pblkcnt = 0;
    835        1.7     skrll 	sc->sc_pbytes = 0;
    836        1.7     skrll 	sc->sc_abytes = blksize;
    837        1.7     skrll 
    838        1.7     skrll 	cv_signal(&sc->sc_datacv);
    839        1.1  jmcneill 
    840        1.1  jmcneill 	return 0;
    841        1.1  jmcneill }
    842        1.2     skrll 
    843        1.1  jmcneill static int
    844        1.1  jmcneill vcaudio_trigger_input(void *priv, void *start, void *end, int blksize,
    845        1.1  jmcneill     void (*intr)(void *), void *intrarg, const audio_params_t *params)
    846        1.1  jmcneill {
    847        1.1  jmcneill 	return EINVAL;
    848        1.1  jmcneill }
    849        1.1  jmcneill 
    850        1.1  jmcneill static void
    851        1.1  jmcneill vcaudio_get_locks(void *priv, kmutex_t **intr, kmutex_t **thread)
    852        1.1  jmcneill {
    853        1.1  jmcneill 	struct vcaudio_softc *sc = priv;
    854        1.1  jmcneill 
    855        1.1  jmcneill 	*intr = &sc->sc_intr_lock;
    856        1.1  jmcneill 	*thread = &sc->sc_lock;
    857        1.1  jmcneill }
    858        1.8  jmcneill 
    859        1.8  jmcneill static void
    860  1.11.16.1  christos vcaudio_swvol_codec(audio_filter_arg_t *arg)
    861        1.8  jmcneill {
    862  1.11.16.1  christos 	struct vcaudio_softc *sc = arg->context;
    863  1.11.16.1  christos 	const aint_t *src;
    864  1.11.16.1  christos 	aint_t *dst;
    865  1.11.16.1  christos 	u_int sample_count;
    866  1.11.16.1  christos 	u_int i;
    867  1.11.16.1  christos 
    868  1.11.16.1  christos 	src = arg->src;
    869  1.11.16.1  christos 	dst = arg->dst;
    870  1.11.16.1  christos 	sample_count = arg->count * arg->srcfmt->channels;
    871  1.11.16.1  christos 	for (i = 0; i < sample_count; i++) {
    872  1.11.16.1  christos 		aint2_t v = (aint2_t)(*src++);
    873  1.11.16.1  christos 		v = v * sc->sc_swvol / 255;
    874  1.11.16.1  christos 		*dst++ = (aint_t)v;
    875  1.11.16.1  christos 	}
    876        1.8  jmcneill }
    877