Home | History | Annotate | Line # | Download | only in broadcom
bcm2835_vcaudio.c revision 1.5
      1 /* $NetBSD: bcm2835_vcaudio.c,v 1.5 2014/09/02 10:40:51 jmcneill 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.5 2014/09/02 10:40:51 jmcneill 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 #include <sys/workqueue.h>
     44 
     45 #include <sys/audioio.h>
     46 #include <dev/audio_if.h>
     47 #include <dev/auconv.h>
     48 
     49 #include <interface/compat/vchi_bsd.h>
     50 #include <interface/vchiq_arm/vchiq_netbsd.h>
     51 #include <interface/vchi/vchi.h>
     52 
     53 #include "bcm2835_vcaudioreg.h"
     54 
     55 #define vol2pct(vol)	(((vol) * 100) / 255)
     56 
     57 enum {
     58 	VCAUDIO_OUTPUT_CLASS,
     59 	VCAUDIO_INPUT_CLASS,
     60 	VCAUDIO_OUTPUT_MASTER_VOLUME,
     61 	VCAUDIO_INPUT_DAC_VOLUME,
     62 	VCAUDIO_ENUM_LAST,
     63 };
     64 
     65 enum vcaudio_dest {
     66 	VCAUDIO_DEST_AUTO = 0,
     67 	VCAUDIO_DEST_HP = 1,
     68 	VCAUDIO_DEST_HDMI = 2,
     69 };
     70 
     71 struct vcaudio_work {
     72 	struct work			vw_wk;
     73 };
     74 
     75 struct vcaudio_softc {
     76 	device_t			sc_dev;
     77 	device_t			sc_audiodev;
     78 
     79 	kmutex_t			sc_lock;
     80 	kmutex_t			sc_intr_lock;
     81 
     82 	kmutex_t			sc_msglock;
     83 	kcondvar_t			sc_msgcv;
     84 
     85 	struct audio_format		sc_format;
     86 	struct audio_encoding_set	*sc_encodings;
     87 
     88 	void				(*sc_pint)(void *);
     89 	void				*sc_pintarg;
     90 	audio_params_t			sc_pparam;
     91 	bool				sc_started;
     92 	int				sc_pbytes;
     93 	off_t				sc_ppos;
     94 	void				*sc_pstart;
     95 	void				*sc_pend;
     96 	int				sc_pblksize;
     97 
     98 	bool				sc_msgdone;
     99 	int				sc_success;
    100 
    101 	VCHI_INSTANCE_T			sc_instance;
    102 	VCHI_CONNECTION_T		sc_connection;
    103 	VCHI_SERVICE_HANDLE_T		sc_service;
    104 
    105 	struct workqueue		*sc_wq;
    106 	struct vcaudio_work		sc_work;
    107 
    108 	int				sc_volume;
    109 	enum vcaudio_dest		sc_dest;
    110 };
    111 
    112 static int	vcaudio_match(device_t, cfdata_t, void *);
    113 static void	vcaudio_attach(device_t, device_t, void *);
    114 static int	vcaudio_rescan(device_t, const char *, const int *);
    115 static void	vcaudio_childdet(device_t, device_t);
    116 
    117 static int	vcaudio_init(struct vcaudio_softc *);
    118 static void	vcaudio_service_callback(void *,
    119 					 const VCHI_CALLBACK_REASON_T,
    120 					 void *);
    121 static int	vcaudio_msg_sync(struct vcaudio_softc *, VC_AUDIO_MSG_T *, size_t);
    122 static void	vcaudio_worker(struct work *, void *);
    123 
    124 static int	vcaudio_open(void *, int);
    125 static void	vcaudio_close(void *);
    126 static int	vcaudio_query_encoding(void *, struct audio_encoding *);
    127 static int	vcaudio_set_params(void *, int, int,
    128 				  audio_params_t *, audio_params_t *,
    129 				  stream_filter_list_t *,
    130 				  stream_filter_list_t *);
    131 static int	vcaudio_halt_output(void *);
    132 static int	vcaudio_halt_input(void *);
    133 static int	vcaudio_set_port(void *, mixer_ctrl_t *);
    134 static int	vcaudio_get_port(void *, mixer_ctrl_t *);
    135 static int	vcaudio_query_devinfo(void *, mixer_devinfo_t *);
    136 static int	vcaudio_getdev(void *, struct audio_device *);
    137 static int	vcaudio_get_props(void *);
    138 static int	vcaudio_round_blocksize(void *, int, int, const audio_params_t *);
    139 static size_t	vcaudio_round_buffersize(void *, int, size_t);
    140 static int	vcaudio_trigger_output(void *, void *, void *, int,
    141 				     void (*)(void *), void *,
    142 				     const audio_params_t *);
    143 static int	vcaudio_trigger_input(void *, void *, void *, int,
    144 				    void (*)(void *), void *,
    145 				    const audio_params_t *);
    146 static void	vcaudio_get_locks(void *, kmutex_t **, kmutex_t **);
    147 
    148 static const struct audio_hw_if vcaudio_hw_if = {
    149 	.open = vcaudio_open,
    150 	.close = vcaudio_close,
    151 	.query_encoding = vcaudio_query_encoding,
    152 	.set_params = vcaudio_set_params,
    153 	.halt_output = vcaudio_halt_output,
    154 	.halt_input = vcaudio_halt_input,
    155 	.getdev = vcaudio_getdev,
    156 	.set_port = vcaudio_set_port,
    157 	.get_port = vcaudio_get_port,
    158 	.query_devinfo = vcaudio_query_devinfo,
    159 	.get_props = vcaudio_get_props,
    160 	.round_blocksize = vcaudio_round_blocksize,
    161 	.round_buffersize = vcaudio_round_buffersize,
    162 	.trigger_output = vcaudio_trigger_output,
    163 	.trigger_input = vcaudio_trigger_input,
    164 	.get_locks = vcaudio_get_locks,
    165 };
    166 
    167 CFATTACH_DECL2_NEW(vcaudio, sizeof(struct vcaudio_softc),
    168     vcaudio_match, vcaudio_attach, NULL, NULL, vcaudio_rescan, vcaudio_childdet);
    169 
    170 static int
    171 vcaudio_match(device_t parent, cfdata_t match, void *aux)
    172 {
    173 	struct vchiq_attach_args *vaa = aux;
    174 
    175 	return !strcmp(vaa->vaa_name, "AUDS");
    176 }
    177 
    178 static void
    179 vcaudio_attach(device_t parent, device_t self, void *aux)
    180 {
    181 	struct vcaudio_softc *sc = device_private(self);
    182 	int error;
    183 
    184 	sc->sc_dev = self;
    185 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    186 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_NONE);
    187 	mutex_init(&sc->sc_msglock, MUTEX_DEFAULT, IPL_NONE);
    188 	cv_init(&sc->sc_msgcv, "vcaudiocv");
    189 	sc->sc_success = -1;
    190 	error = workqueue_create(&sc->sc_wq, "vcaudiowq", vcaudio_worker,
    191 	    sc, PRI_BIO, IPL_SCHED, WQ_MPSAFE);
    192 	if (error) {
    193 		aprint_error(": couldn't create workqueue (%d)\n", error);
    194 		return;
    195 	}
    196 
    197 	aprint_naive("\n");
    198 	aprint_normal(": AUDS\n");
    199 
    200 	if (vcaudio_init(sc) != 0) {
    201 		aprint_error_dev(self, "not configured\n");
    202 		return;
    203 	}
    204 
    205 	vcaudio_rescan(self, NULL, NULL);
    206 }
    207 
    208 static int
    209 vcaudio_rescan(device_t self, const char *ifattr, const int *locs)
    210 {
    211 	struct vcaudio_softc *sc = device_private(self);
    212 
    213 	if (ifattr_match(ifattr, "audiobus") && sc->sc_audiodev == NULL) {
    214 		sc->sc_audiodev = audio_attach_mi(&vcaudio_hw_if,
    215 		    sc, sc->sc_dev);
    216 	}
    217 	return 0;
    218 }
    219 
    220 static void
    221 vcaudio_childdet(device_t self, device_t child)
    222 {
    223 	struct vcaudio_softc *sc = device_private(self);
    224 
    225 	if (sc->sc_audiodev == child)
    226 		sc->sc_audiodev = NULL;
    227 }
    228 
    229 static int
    230 vcaudio_init(struct vcaudio_softc *sc)
    231 {
    232 	VC_AUDIO_MSG_T msg;
    233 	int error;
    234 
    235 	sc->sc_volume = 128;
    236 	sc->sc_dest = VCAUDIO_DEST_AUTO;
    237 
    238 	sc->sc_format.mode = AUMODE_PLAY|AUMODE_RECORD;
    239 	sc->sc_format.encoding = AUDIO_ENCODING_SLINEAR_LE;
    240 	sc->sc_format.validbits = 16;
    241 	sc->sc_format.precision = 16;
    242 	sc->sc_format.channels = 2;
    243 	sc->sc_format.channel_mask = AUFMT_STEREO;
    244 	sc->sc_format.frequency_type = 0;
    245 	sc->sc_format.frequency[0] = 48000;
    246 	sc->sc_format.frequency[1] = 48000;
    247 
    248 	error = auconv_create_encodings(&sc->sc_format, 1, &sc->sc_encodings);
    249 	if (error) {
    250 		aprint_error_dev(sc->sc_dev,
    251 		    "couldn't create encodings (error=%d)\n", error);
    252 		return error;
    253 	}
    254 
    255 	error = vchi_initialise(&sc->sc_instance);
    256 	if (error) {
    257 		device_printf(sc->sc_dev, "couldn't init vchi instance (%d)\n",
    258 		    error);
    259 		return EIO;
    260 	}
    261 
    262 	error = vchi_connect(NULL, 0, sc->sc_instance);
    263 	if (error) {
    264 		device_printf(sc->sc_dev, "couldn't connect vchi (%d)\n",
    265 		    error);
    266 		return EIO;
    267 	}
    268 
    269 	SERVICE_CREATION_T setup = {
    270 		.version = VCHI_VERSION(VC_AUDIOSERV_VER),
    271 		.service_id = VC_AUDIO_SERVER_NAME,
    272 		.connection = &sc->sc_connection,
    273 		.rx_fifo_size = 0,
    274 		.tx_fifo_size = 0,
    275 		.callback = vcaudio_service_callback,
    276 		.callback_param = sc,
    277 		.want_unaligned_bulk_rx = 1,
    278 		.want_unaligned_bulk_tx = 1,
    279 		.want_crc = 0,
    280 	};
    281 
    282 	error = vchi_service_open(sc->sc_instance, &setup, &sc->sc_service);
    283 	if (error) {
    284 		device_printf(sc->sc_dev, "couldn't open service (%d)\n",
    285 		    error);
    286 		return EIO;
    287 	}
    288 	vchi_service_release(sc->sc_service);
    289 
    290 	vchi_service_use(sc->sc_service);
    291 	memset(&msg, 0, sizeof(msg));
    292 	msg.type = VC_AUDIO_MSG_TYPE_OPEN;
    293 	error = vchi_msg_queue(sc->sc_service, &msg, sizeof(msg),
    294 	    VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
    295 	if (error) {
    296 		device_printf(sc->sc_dev, "couldn't send OPEN message (%d)\n",
    297 		    error);
    298 	}
    299 
    300 	memset(&msg, 0, sizeof(msg));
    301 	msg.type = VC_AUDIO_MSG_TYPE_CONFIG;
    302 	msg.u.config.channels = 2;
    303 	msg.u.config.samplerate = 48000;
    304 	msg.u.config.bps = 16;
    305 	error = vcaudio_msg_sync(sc, &msg, sizeof(msg));
    306 	if (error) {
    307 		device_printf(sc->sc_dev, "couldn't send CONFIG message (%d)\n",
    308 		    error);
    309 	}
    310 
    311 	memset(&msg, 0, sizeof(msg));
    312 	msg.type = VC_AUDIO_MSG_TYPE_CONTROL;
    313 	msg.u.control.volume = vol2pct(sc->sc_volume);
    314 	msg.u.control.dest = VCAUDIO_DEST_AUTO;
    315 	error = vcaudio_msg_sync(sc, &msg, sizeof(msg));
    316 	if (error) {
    317 		device_printf(sc->sc_dev, "couldn't send CONTROL message (%d)\n", error);
    318 	}
    319 	vchi_service_release(sc->sc_service);
    320 
    321 	return 0;
    322 }
    323 
    324 static void
    325 vcaudio_service_callback(void *priv, const VCHI_CALLBACK_REASON_T reason,
    326     void *msg_handle)
    327 {
    328 	struct vcaudio_softc *sc = priv;
    329 	VC_AUDIO_MSG_T msg;
    330 	int32_t msglen = 0;
    331 	int error;
    332 	void (*intr)(void *) = NULL;
    333 	void *intrarg = NULL;
    334 
    335 	if (sc == NULL || reason != VCHI_CALLBACK_MSG_AVAILABLE)
    336 		return;
    337 
    338 	memset(&msg, 0, sizeof(msg));
    339 	error = vchi_msg_dequeue(sc->sc_service, &msg, sizeof(msg), &msglen,
    340 	    VCHI_FLAGS_NONE);
    341 	if (error) {
    342 		device_printf(sc->sc_dev, "couldn't dequeue msg (%d)\n",
    343 		    error);
    344 		return;
    345 	}
    346 
    347 	switch (msg.type) {
    348 	case VC_AUDIO_MSG_TYPE_RESULT:
    349 		mutex_enter(&sc->sc_msglock);
    350 		sc->sc_success = msg.u.result.success;
    351 		sc->sc_msgdone = true;
    352 		cv_broadcast(&sc->sc_msgcv);
    353 		mutex_exit(&sc->sc_msglock);
    354 		break;
    355 	case VC_AUDIO_MSG_TYPE_COMPLETE:
    356 		intr = msg.u.complete.callback;
    357 		intrarg = msg.u.complete.cookie;
    358 		if (intr && intrarg) {
    359 			int count = msg.u.complete.count & 0xffff;
    360 			int perr = (msg.u.complete.count & 0x40000000) != 0;
    361 			bool sched = false;
    362 			mutex_enter(&sc->sc_intr_lock);
    363 			if (count > 0) {
    364 				sc->sc_pbytes += count;
    365 			}
    366 			if (perr && sc->sc_started) {
    367 #ifdef VCAUDIO_DEBUG
    368 				device_printf(sc->sc_dev, "underrun\n");
    369 #endif
    370 				sched = true;
    371 			}
    372 			if (sc->sc_pbytes >= sc->sc_pblksize) {
    373 				sc->sc_pbytes -= sc->sc_pblksize;
    374 				sched = true;
    375 			}
    376 
    377 			if (sched) {
    378 				intr(intrarg);
    379 				workqueue_enqueue(sc->sc_wq, (struct work *)&sc->sc_work, NULL);
    380 			}
    381 			mutex_exit(&sc->sc_intr_lock);
    382 		}
    383 		break;
    384 	default:
    385 		break;
    386 	}
    387 }
    388 
    389 static void
    390 vcaudio_worker(struct work *wk, void *priv)
    391 {
    392 	struct vcaudio_softc *sc = priv;
    393 	VC_AUDIO_MSG_T msg;
    394 	void (*intr)(void *);
    395 	void *intrarg;
    396 	void *block;
    397 	int error, resid, off, nb, count;
    398 
    399 	mutex_enter(&sc->sc_intr_lock);
    400 	intr = sc->sc_pint;
    401 	intrarg = sc->sc_pintarg;
    402 
    403 	if (intr == NULL || intrarg == NULL) {
    404 		mutex_exit(&sc->sc_intr_lock);
    405 		return;
    406 	}
    407 
    408 	vchi_service_use(sc->sc_service);
    409 
    410 	if (sc->sc_started == false) {
    411 #ifdef VCAUDIO_DEBUG
    412 		device_printf(sc->sc_dev, "starting output\n");
    413 #endif
    414 
    415 		memset(&msg, 0, sizeof(msg));
    416 		msg.type = VC_AUDIO_MSG_TYPE_START;
    417 		error = vchi_msg_queue(sc->sc_service, &msg, sizeof(msg),
    418 		    VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
    419 		if (error) {
    420 			printf("%s: failed to start (%d)\n", __func__, error);
    421 			goto done;
    422 		}
    423 
    424 		sc->sc_started = true;
    425 		sc->sc_pbytes = 0;
    426 		sc->sc_ppos = 0;
    427 
    428 		count = sc->sc_pblksize * 2;
    429 	} else {
    430 		count = sc->sc_pblksize;
    431 	}
    432 
    433 	memset(&msg, 0, sizeof(msg));
    434 	msg.type = VC_AUDIO_MSG_TYPE_WRITE;
    435 	msg.u.write.max_packet = 4000;
    436 	msg.u.write.count = count;
    437 	msg.u.write.callback = intr;
    438 	msg.u.write.cookie = intrarg;
    439 	msg.u.write.silence = 0;
    440 
    441 	error = vchi_msg_queue(sc->sc_service, &msg, sizeof(msg),
    442 	    VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
    443 	if (error) {
    444 		printf("%s: failed to write (%d)\n", __func__, error);
    445 		goto done;
    446 	}
    447 
    448 	block = (uint8_t *)sc->sc_pstart + sc->sc_ppos;
    449 	resid = count;
    450 	off = 0;
    451 	while (resid > 0) {
    452 		nb = min(resid, msg.u.write.max_packet);
    453 		error = vchi_msg_queue(sc->sc_service,
    454 		    (char *)block + off, nb,
    455 		    VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
    456 		if (error) {
    457 			printf("%s: failed to queue data (%d)\n", __func__, error);
    458 			goto done;
    459 		}
    460 		off += nb;
    461 		resid -= nb;
    462 	}
    463 
    464 	sc->sc_ppos += count;
    465 	if ((uint8_t *)sc->sc_pstart + sc->sc_ppos >= (uint8_t *)sc->sc_pend)
    466 		sc->sc_ppos = 0;
    467 
    468 done:
    469 	mutex_exit(&sc->sc_intr_lock);
    470 	vchi_service_release(sc->sc_service);
    471 }
    472 
    473 static int
    474 vcaudio_msg_sync(struct vcaudio_softc *sc, VC_AUDIO_MSG_T *msg, size_t msglen)
    475 {
    476 	int error = 0;
    477 
    478 	mutex_enter(&sc->sc_msglock);
    479 
    480 	sc->sc_success = -1;
    481 	sc->sc_msgdone = false;
    482 
    483 	error = vchi_msg_queue(sc->sc_service, msg, msglen,
    484 	    VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
    485 	if (error) {
    486 		printf("%s: failed to queue message (%d)\n", __func__, error);
    487 		goto done;
    488 	}
    489 
    490 	while (!sc->sc_msgdone) {
    491 		error = cv_wait_sig(&sc->sc_msgcv, &sc->sc_msglock);
    492 		if (error)
    493 			break;
    494 	}
    495 	if (sc->sc_success != 0)
    496 		error = EIO;
    497 done:
    498 	mutex_exit(&sc->sc_msglock);
    499 
    500 	return error;
    501 }
    502 
    503 static int
    504 vcaudio_open(void *priv, int flags)
    505 {
    506 	return 0;
    507 }
    508 
    509 static void
    510 vcaudio_close(void *priv)
    511 {
    512 }
    513 
    514 static int
    515 vcaudio_query_encoding(void *priv, struct audio_encoding *ae)
    516 {
    517 	struct vcaudio_softc *sc = priv;
    518 
    519 	return auconv_query_encoding(sc->sc_encodings, ae);
    520 }
    521 
    522 static int
    523 vcaudio_set_params(void *priv, int setmode, int usemode,
    524     audio_params_t *play, audio_params_t *rec,
    525     stream_filter_list_t *pfil, stream_filter_list_t *rfil)
    526 {
    527 	struct vcaudio_softc *sc = priv;
    528 	int index;
    529 
    530 	if (play && (setmode & AUMODE_PLAY)) {
    531 		index = auconv_set_converter(&sc->sc_format, 1,
    532 		    AUMODE_PLAY, play, true, pfil);
    533 		if (index < 0)
    534 			return EINVAL;
    535 	}
    536 
    537 	return 0;
    538 }
    539 
    540 static int
    541 vcaudio_halt_output(void *priv)
    542 {
    543 	struct vcaudio_softc *sc = priv;
    544 	VC_AUDIO_MSG_T msg;
    545 	int error = 0;
    546 
    547 	vchi_service_use(sc->sc_service);
    548 	memset(&msg, 0, sizeof(msg));
    549 	msg.type = VC_AUDIO_MSG_TYPE_STOP;
    550 	msg.u.stop.draining = 1;
    551 	error = vchi_msg_queue(sc->sc_service, &msg, sizeof(msg),
    552 	    VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
    553 	if (error) {
    554 		device_printf(sc->sc_dev, "couldn't send STOP message (%d)\n",
    555 		    error);
    556 	}
    557 	vchi_service_release(sc->sc_service);
    558 
    559 	sc->sc_pint = NULL;
    560 	sc->sc_pintarg = NULL;
    561 	sc->sc_started = false;
    562 
    563 #ifdef VCAUDIO_DEBUG
    564 	device_printf(sc->sc_dev, "halting output\n");
    565 #endif
    566 
    567 	return error;
    568 }
    569 
    570 static int
    571 vcaudio_halt_input(void *priv)
    572 {
    573 	return EINVAL;
    574 }
    575 
    576 static int
    577 vcaudio_set_port(void *priv, mixer_ctrl_t *mc)
    578 {
    579 	struct vcaudio_softc *sc = priv;
    580 	VC_AUDIO_MSG_T msg;
    581 	int error;
    582 
    583 	switch (mc->dev) {
    584 	case VCAUDIO_OUTPUT_MASTER_VOLUME:
    585 	case VCAUDIO_INPUT_DAC_VOLUME:
    586 		sc->sc_volume = mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
    587 		memset(&msg, 0, sizeof(msg));
    588 		msg.type = VC_AUDIO_MSG_TYPE_CONTROL;
    589 		msg.u.control.volume = vol2pct(sc->sc_volume);
    590 		msg.u.control.dest = VCAUDIO_DEST_AUTO;
    591 		vchi_service_use(sc->sc_service);
    592 		error = vcaudio_msg_sync(sc, &msg, sizeof(msg));
    593 		if (error) {
    594 			device_printf(sc->sc_dev, "couldn't send CONTROL message (%d)\n", error);
    595 		}
    596 		vchi_service_release(sc->sc_service);
    597 
    598 		return error;
    599 	}
    600 	return ENXIO;
    601 }
    602 
    603 static int
    604 vcaudio_get_port(void *priv, mixer_ctrl_t *mc)
    605 {
    606 	struct vcaudio_softc *sc = priv;
    607 
    608 	switch (mc->dev) {
    609 	case VCAUDIO_OUTPUT_MASTER_VOLUME:
    610 	case VCAUDIO_INPUT_DAC_VOLUME:
    611 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] =
    612 		    mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] =
    613 		    sc->sc_volume;
    614 		return 0;
    615 	}
    616 	return ENXIO;
    617 }
    618 
    619 static int
    620 vcaudio_query_devinfo(void *priv, mixer_devinfo_t *di)
    621 {
    622 	switch (di->index) {
    623 	case VCAUDIO_OUTPUT_CLASS:
    624 		di->mixer_class = VCAUDIO_OUTPUT_CLASS;
    625 		strcpy(di->label.name, AudioCoutputs);
    626 		di->type = AUDIO_MIXER_CLASS;
    627 		di->next = di->prev = AUDIO_MIXER_LAST;
    628 		return 0;
    629 	case VCAUDIO_INPUT_CLASS:
    630 		di->mixer_class = VCAUDIO_INPUT_CLASS;
    631 		strcpy(di->label.name, AudioCinputs);
    632 		di->type = AUDIO_MIXER_CLASS;
    633 		di->next = di->prev = AUDIO_MIXER_LAST;
    634 		return 0;
    635 	case VCAUDIO_OUTPUT_MASTER_VOLUME:
    636 		di->mixer_class = VCAUDIO_OUTPUT_CLASS;
    637 		strcpy(di->label.name, AudioNmaster);
    638 		di->type = AUDIO_MIXER_VALUE;
    639 		di->next = di->prev = AUDIO_MIXER_LAST;
    640 		di->un.v.num_channels = 2;
    641 		strcpy(di->un.v.units.name, AudioNvolume);
    642 		return 0;
    643 	case VCAUDIO_INPUT_DAC_VOLUME:
    644 		di->mixer_class = VCAUDIO_INPUT_CLASS;
    645 		strcpy(di->label.name, AudioNdac);
    646 		di->type = AUDIO_MIXER_VALUE;
    647 		di->next = di->prev = AUDIO_MIXER_LAST;
    648 		di->un.v.num_channels = 2;
    649 		strcpy(di->un.v.units.name, AudioNvolume);
    650 		return 0;
    651 	}
    652 
    653 	return ENXIO;
    654 }
    655 
    656 static int
    657 vcaudio_getdev(void *priv, struct audio_device *audiodev)
    658 {
    659 	snprintf(audiodev->name, sizeof(audiodev->name), "VCHIQ AUDS");
    660 	snprintf(audiodev->version, sizeof(audiodev->version), "");
    661 	snprintf(audiodev->config, sizeof(audiodev->config), "vcaudio");
    662 	return 0;
    663 }
    664 
    665 static int
    666 vcaudio_get_props(void *priv)
    667 {
    668 	return AUDIO_PROP_PLAYBACK|AUDIO_PROP_CAPTURE|AUDIO_PROP_INDEPENDENT;
    669 }
    670 
    671 static int
    672 vcaudio_round_blocksize(void *priv, int bs, int mode,
    673     const audio_params_t *params)
    674 {
    675 	return PAGE_SIZE;
    676 }
    677 
    678 static size_t
    679 vcaudio_round_buffersize(void *priv, int direction, size_t bufsize)
    680 {
    681 	size_t sz;
    682 
    683 	sz = (bufsize + 0x3ff) & ~0x3ff;
    684 	if (sz > 32768)
    685 		sz = 32768;
    686 
    687 	return sz;
    688 }
    689 
    690 static int
    691 vcaudio_trigger_output(void *priv, void *start, void *end, int blksize,
    692     void (*intr)(void *), void *intrarg, const audio_params_t *params)
    693 {
    694 	struct vcaudio_softc *sc = priv;
    695 
    696 	sc->sc_pparam = *params;
    697 	sc->sc_pint = intr;
    698 	sc->sc_pintarg = intrarg;
    699 	sc->sc_ppos = 0;
    700 	sc->sc_pstart = start;
    701 	sc->sc_pend = end;
    702 	sc->sc_pblksize = blksize;
    703 	workqueue_enqueue(sc->sc_wq, (struct work *)&sc->sc_work, NULL);
    704 
    705 	return 0;
    706 }
    707 
    708 static int
    709 vcaudio_trigger_input(void *priv, void *start, void *end, int blksize,
    710     void (*intr)(void *), void *intrarg, const audio_params_t *params)
    711 {
    712 	return EINVAL;
    713 }
    714 
    715 static void
    716 vcaudio_get_locks(void *priv, kmutex_t **intr, kmutex_t **thread)
    717 {
    718 	struct vcaudio_softc *sc = priv;
    719 
    720 	*intr = &sc->sc_intr_lock;
    721 	*thread = &sc->sc_lock;
    722 }
    723