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