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