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