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