Home | History | Annotate | Line # | Download | only in dev
vs.c revision 1.38
      1 /*	$NetBSD: vs.c,v 1.38 2017/06/25 06:26:40 isaki Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001 Tetsuya Isaki. All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     20  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     22  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     23  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  * SUCH DAMAGE.
     26  */
     27 
     28 /*
     29  * VS - OKI MSM6258 ADPCM voice synthesizer device driver.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: vs.c,v 1.38 2017/06/25 06:26:40 isaki Exp $");
     34 
     35 #include "audio.h"
     36 #include "vs.h"
     37 #if NAUDIO > 0 && NVS > 0
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/device.h>
     42 #include <sys/kmem.h>
     43 
     44 #include <sys/audioio.h>
     45 #include <dev/audio_if.h>
     46 #include <dev/mulaw.h>
     47 
     48 #include <machine/bus.h>
     49 #include <machine/cpu.h>
     50 
     51 #include <dev/ic/msm6258var.h>
     52 
     53 #include <arch/x68k/dev/dmacvar.h>
     54 #include <arch/x68k/dev/intiovar.h>
     55 #include <arch/x68k/dev/opmvar.h>
     56 
     57 #include <arch/x68k/dev/vsvar.h>
     58 
     59 #ifdef VS_DEBUG
     60 #define DPRINTF(y,x)	if (vs_debug >= (y)) printf x
     61 static int vs_debug;
     62 #ifdef AUDIO_DEBUG
     63 extern int audiodebug;
     64 #endif
     65 #else
     66 #define DPRINTF(y,x)
     67 #endif
     68 
     69 static int  vs_match(device_t, cfdata_t, void *);
     70 static void vs_attach(device_t, device_t, void *);
     71 
     72 static int  vs_dmaintr(void *);
     73 static int  vs_dmaerrintr(void *);
     74 
     75 /* MI audio layer interface */
     76 static int  vs_open(void *, int);
     77 static void vs_close(void *);
     78 static int  vs_query_encoding(void *, struct audio_encoding *);
     79 static int  vs_set_params(void *, int, int, audio_params_t *,
     80 	audio_params_t *, stream_filter_list_t *, stream_filter_list_t *);
     81 static int  vs_trigger_output(void *, void *, void *, int,
     82 	void (*)(void *), void *, const audio_params_t *);
     83 static int  vs_trigger_input(void *, void *, void *, int,
     84 	void (*)(void *), void *, const audio_params_t *);
     85 static int  vs_halt_output(void *);
     86 static int  vs_halt_input(void *);
     87 static int  vs_allocmem(struct vs_softc *, size_t, size_t, size_t,
     88 	struct vs_dma *);
     89 static void vs_freemem(struct vs_dma *);
     90 static int  vs_getdev(void *, struct audio_device *);
     91 static int  vs_set_port(void *, mixer_ctrl_t *);
     92 static int  vs_get_port(void *, mixer_ctrl_t *);
     93 static int  vs_query_devinfo(void *, mixer_devinfo_t *);
     94 static void *vs_allocm(void *, int, size_t);
     95 static void vs_freem(void *, void *, size_t);
     96 static size_t vs_round_buffersize(void *, int, size_t);
     97 static int  vs_get_props(void *);
     98 static void vs_get_locks(void *, kmutex_t **, kmutex_t **);
     99 
    100 /* lower functions */
    101 static int vs_round_sr(u_long);
    102 static void vs_set_sr(struct vs_softc *, int);
    103 static inline void vs_set_po(struct vs_softc *, u_long);
    104 
    105 extern struct cfdriver vs_cd;
    106 
    107 CFATTACH_DECL_NEW(vs, sizeof(struct vs_softc),
    108     vs_match, vs_attach, NULL, NULL);
    109 
    110 static int vs_attached;
    111 
    112 static const struct audio_hw_if vs_hw_if = {
    113 	vs_open,
    114 	vs_close,
    115 	NULL,			/* drain */
    116 	vs_query_encoding,
    117 	vs_set_params,
    118 	NULL,			/* round_blocksize */
    119 	NULL,			/* commit_settings */
    120 	NULL,			/* init_output */
    121 	NULL,			/* init_input */
    122 	NULL,			/* start_output */
    123 	NULL,			/* start_input */
    124 	vs_halt_output,
    125 	vs_halt_input,
    126 	NULL,			/* speaker_ctl */
    127 	vs_getdev,
    128 	NULL,			/* setfd */
    129 	vs_set_port,
    130 	vs_get_port,
    131 	vs_query_devinfo,
    132 	vs_allocm,
    133 	vs_freem,
    134 	vs_round_buffersize,
    135 	NULL,			/* mappage */
    136 	vs_get_props,
    137 	vs_trigger_output,
    138 	vs_trigger_input,
    139 	NULL,
    140 	vs_get_locks,
    141 };
    142 
    143 static struct audio_device vs_device = {
    144 	"OKI MSM6258",
    145 	"",
    146 	"vs"
    147 };
    148 
    149 struct {
    150 	u_long rate;
    151 	u_char clk;
    152 	u_char den;
    153 } vs_l2r[] = {
    154 	{ VS_RATE_15K, VS_CLK_8MHZ, VS_SRATE_512 },
    155 	{ VS_RATE_10K, VS_CLK_8MHZ, VS_SRATE_768 },
    156 	{ VS_RATE_7K,  VS_CLK_8MHZ, VS_SRATE_1024},
    157 	{ VS_RATE_5K,  VS_CLK_4MHZ, VS_SRATE_768 },
    158 	{ VS_RATE_3K,  VS_CLK_4MHZ, VS_SRATE_1024}
    159 };
    160 
    161 #define NUM_RATE	(sizeof(vs_l2r)/sizeof(vs_l2r[0]))
    162 
    163 struct {
    164 	const char *name;
    165 	int	encoding;
    166 	int	precision;
    167 } vs_encodings[] = {
    168 	{AudioEadpcm,      AUDIO_ENCODING_ADPCM,       4},
    169 	{AudioEslinear,    AUDIO_ENCODING_SLINEAR,     8},
    170 	{AudioEulinear,    AUDIO_ENCODING_ULINEAR,     8},
    171 	{AudioEmulaw,      AUDIO_ENCODING_ULAW,	       8},
    172 	{AudioEslinear_be, AUDIO_ENCODING_SLINEAR_BE, 16},
    173 	{AudioEslinear_le, AUDIO_ENCODING_SLINEAR_LE, 16},
    174 };
    175 
    176 static int
    177 vs_match(device_t parent, cfdata_t cf, void *aux)
    178 {
    179 	struct intio_attach_args *ia;
    180 
    181 	ia = aux;
    182 	if (strcmp(ia->ia_name, "vs") || vs_attached)
    183 		return 0;
    184 
    185 	if (ia->ia_addr == INTIOCF_ADDR_DEFAULT)
    186 		ia->ia_addr = VS_ADDR;
    187 	if (ia->ia_dma == INTIOCF_DMA_DEFAULT)
    188 		ia->ia_dma = VS_DMA;
    189 	if (ia->ia_dmaintr == INTIOCF_DMAINTR_DEFAULT)
    190 		ia->ia_dmaintr = VS_DMAINTR;
    191 
    192 	/* fixed parameters */
    193 	if (ia->ia_addr != VS_ADDR)
    194 		return 0;
    195 	if (ia->ia_dma != VS_DMA)
    196 		return 0;
    197 	if (ia->ia_dmaintr != VS_DMAINTR)
    198 		return 0;
    199 
    200 #ifdef VS_DEBUG
    201 	vs_debug = 1;
    202 #ifdef AUDIO_DEBUG
    203 	audiodebug = 2;
    204 #endif
    205 #endif
    206 
    207 	return 1;
    208 }
    209 
    210 static void
    211 vs_attach(device_t parent, device_t self, void *aux)
    212 {
    213 	struct vs_softc *sc;
    214 	bus_space_tag_t iot;
    215 	bus_space_handle_t ioh;
    216 	struct intio_attach_args *ia;
    217 
    218 	sc = device_private(self);
    219 	sc->sc_dev = self;
    220 	ia = aux;
    221 	vs_attached = 1;
    222 
    223 	printf("\n");
    224 
    225 	/* Re-map the I/O space */
    226 	iot = ia->ia_bst;
    227 	bus_space_map(iot, ia->ia_addr, 0x2000, BUS_SPACE_MAP_SHIFTED, &ioh);
    228 
    229 	/* Initialize sc */
    230 	sc->sc_iot = iot;
    231 	sc->sc_ioh = ioh;
    232 	sc->sc_hw_if = &vs_hw_if;
    233 	sc->sc_addr = (void *) ia->ia_addr;
    234 	sc->sc_dmas = NULL;
    235 	sc->sc_active = 0;
    236 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    237 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SCHED);
    238 
    239 	/* XXX */
    240 	bus_space_map(iot, PPI_ADDR, PPI_MAPSIZE, BUS_SPACE_MAP_SHIFTED,
    241 		      &sc->sc_ppi);
    242 
    243 	/* Initialize DMAC */
    244 	sc->sc_dmat = ia->ia_dmat;
    245 	sc->sc_dma_ch = dmac_alloc_channel(parent, ia->ia_dma, "vs",
    246 		ia->ia_dmaintr,   vs_dmaintr, sc,
    247 		ia->ia_dmaintr+1, vs_dmaerrintr, sc);
    248 
    249 	aprint_normal_dev(self, "MSM6258V ADPCM voice synthesizer\n");
    250 
    251 	audio_attach_mi(&vs_hw_if, sc, sc->sc_dev);
    252 }
    253 
    254 /*
    255  * vs interrupt handler
    256  */
    257 static int
    258 vs_dmaintr(void *hdl)
    259 {
    260 	struct vs_softc *sc;
    261 
    262 	DPRINTF(2, ("vs_dmaintr\n"));
    263 	sc = hdl;
    264 
    265 	mutex_spin_enter(&sc->sc_intr_lock);
    266 
    267 	if (sc->sc_pintr) {
    268 		/* start next transfer */
    269 		sc->sc_current.dmap += sc->sc_current.blksize;
    270 		if (sc->sc_current.dmap + sc->sc_current.blksize
    271 		    > sc->sc_current.bufsize)
    272 			sc->sc_current.dmap -= sc->sc_current.bufsize;
    273 		dmac_start_xfer_offset(sc->sc_dma_ch->ch_softc,
    274 					sc->sc_current.xfer,
    275 					sc->sc_current.dmap,
    276 					sc->sc_current.blksize);
    277 		sc->sc_pintr(sc->sc_parg);
    278 	} else if (sc->sc_rintr) {
    279 		/* start next transfer */
    280 		sc->sc_current.dmap += sc->sc_current.blksize;
    281 		if (sc->sc_current.dmap + sc->sc_current.blksize
    282 		    > sc->sc_current.bufsize)
    283 			sc->sc_current.dmap -= sc->sc_current.bufsize;
    284 		dmac_start_xfer_offset(sc->sc_dma_ch->ch_softc,
    285 					sc->sc_current.xfer,
    286 					sc->sc_current.dmap,
    287 					sc->sc_current.blksize);
    288 		sc->sc_rintr(sc->sc_rarg);
    289 	} else {
    290 		printf("vs_dmaintr: spurious interrupt\n");
    291 	}
    292 
    293 	mutex_spin_exit(&sc->sc_intr_lock);
    294 
    295 	return 1;
    296 }
    297 
    298 static int
    299 vs_dmaerrintr(void *hdl)
    300 {
    301 	struct vs_softc *sc;
    302 
    303 	sc = hdl;
    304 	DPRINTF(1, ("%s: DMA transfer error.\n", device_xname(sc->sc_dev)));
    305 	/* XXX */
    306 	vs_dmaintr(sc);
    307 
    308 	return 1;
    309 }
    310 
    311 
    312 /*
    313  * audio MD layer interfaces
    314  */
    315 
    316 static int
    317 vs_open(void *hdl, int flags)
    318 {
    319 	struct vs_softc *sc;
    320 
    321 	DPRINTF(1, ("vs_open: flags=%d\n", flags));
    322 	sc = hdl;
    323 	sc->sc_pintr = NULL;
    324 	sc->sc_rintr = NULL;
    325 
    326 	return 0;
    327 }
    328 
    329 static void
    330 vs_close(void *hdl)
    331 {
    332 
    333 	DPRINTF(1, ("vs_close\n"));
    334 }
    335 
    336 static int
    337 vs_query_encoding(void *hdl, struct audio_encoding *fp)
    338 {
    339 
    340 	DPRINTF(1, ("vs_query_encoding\n"));
    341 	if (fp->index >= sizeof(vs_encodings) / sizeof(vs_encodings[0]))
    342 		return EINVAL;
    343 
    344 	strcpy(fp->name, vs_encodings[fp->index].name);
    345 	fp->encoding  = vs_encodings[fp->index].encoding;
    346 	fp->precision = vs_encodings[fp->index].precision;
    347 	if (fp->encoding == AUDIO_ENCODING_ADPCM)
    348 		fp->flags = 0;
    349 	else
    350 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    351 	return 0;
    352 }
    353 
    354 static int
    355 vs_round_sr(u_long rate)
    356 {
    357 	int i;
    358 	int diff;
    359 	int nearest;
    360 
    361 	diff = rate;
    362 	nearest = 0;
    363 	for (i = 0; i < NUM_RATE; i++) {
    364 		if (rate >= vs_l2r[i].rate) {
    365 			if (rate - vs_l2r[i].rate < diff) {
    366 				diff = rate - vs_l2r[i].rate;
    367 				nearest = i;
    368 			}
    369 		} else {
    370 			if (vs_l2r[i].rate - rate < diff) {
    371 				diff = vs_l2r[i].rate - rate;
    372 				nearest = i;
    373 			}
    374 		}
    375 	}
    376 	if (diff * 100 / rate > 15)
    377 		return -1;
    378 	else
    379 		return nearest;
    380 }
    381 
    382 static int
    383 vs_set_params(void *hdl, int setmode, int usemode,
    384 	audio_params_t *play, audio_params_t *rec,
    385 	stream_filter_list_t *pfil, stream_filter_list_t *rfil)
    386 {
    387 	struct vs_softc *sc;
    388 	struct audio_params *p;
    389 	int mode;
    390 	int rate;
    391 	audio_params_t hw;
    392 	int matched;
    393 
    394 	DPRINTF(1, ("vs_set_params: setmode=%d, usemode=%d\n",
    395 		setmode, usemode));
    396 
    397 	sc = hdl;
    398 	/* set first record info, then play info */
    399 	for (mode = AUMODE_RECORD; mode != -1;
    400 	     mode = (mode == AUMODE_RECORD) ? AUMODE_PLAY : -1) {
    401 		if ((setmode & mode) == 0)
    402 			continue;
    403 
    404 		p = (mode == AUMODE_PLAY) ? play : rec;
    405 
    406 		if (p->channels != 1)
    407 			return EINVAL;
    408 
    409 		rate = p->sample_rate;
    410 		hw = *p;
    411 		hw.encoding = AUDIO_ENCODING_ADPCM;
    412 		hw.precision = hw.validbits = 4;
    413 		DPRINTF(1, ("vs_set_params: encoding=%d, precision=%d\n",
    414 			p->encoding, p->precision));
    415 		matched = 0;
    416 		switch (p->precision) {
    417 		case 4:
    418 			if (p->encoding == AUDIO_ENCODING_ADPCM)
    419 				matched = 1;
    420 			break;
    421 		case 8:
    422 			switch (p->encoding) {
    423 			case AUDIO_ENCODING_ULAW:
    424 				matched = 1;
    425 				hw.encoding = AUDIO_ENCODING_ULINEAR_LE;
    426 				hw.precision = hw.validbits = 8;
    427 				pfil->prepend(pfil, mulaw_to_linear8, &hw);
    428 				hw.encoding = AUDIO_ENCODING_ADPCM;
    429 				hw.precision = hw.validbits = 4;
    430 				pfil->prepend(pfil, msm6258_linear8_to_adpcm, &hw);
    431 				rfil->append(rfil, msm6258_adpcm_to_linear8, &hw);
    432 				hw.encoding = AUDIO_ENCODING_ULINEAR_LE;
    433 				hw.precision = hw.validbits = 8;
    434 				rfil->append(rfil, linear8_to_mulaw, &hw);
    435 				break;
    436 			case AUDIO_ENCODING_SLINEAR:
    437 			case AUDIO_ENCODING_SLINEAR_LE:
    438 			case AUDIO_ENCODING_SLINEAR_BE:
    439 			case AUDIO_ENCODING_ULINEAR:
    440 			case AUDIO_ENCODING_ULINEAR_LE:
    441 			case AUDIO_ENCODING_ULINEAR_BE:
    442 				matched = 1;
    443 				pfil->append(pfil, msm6258_linear8_to_adpcm, &hw);
    444 				rfil->append(rfil, msm6258_adpcm_to_linear8, &hw);
    445 				break;
    446 			}
    447 			break;
    448 		case 16:
    449 			switch (p->encoding) {
    450 			case AUDIO_ENCODING_SLINEAR_LE:
    451 			case AUDIO_ENCODING_SLINEAR_BE:
    452 				matched = 1;
    453 				pfil->append(pfil, msm6258_slinear16_to_adpcm, &hw);
    454 				rfil->append(rfil, msm6258_adpcm_to_slinear16, &hw);
    455 				break;
    456 			}
    457 			break;
    458 		}
    459 		if (matched == 0) {
    460 			DPRINTF(1, ("vs_set_params: mode=%d, encoding=%d\n",
    461 				mode, p->encoding));
    462 			return EINVAL;
    463 		}
    464 
    465 		DPRINTF(1, ("vs_set_params: rate=%d -> ", rate));
    466 		rate = vs_round_sr(rate);
    467 		DPRINTF(1, ("%d\n", rate));
    468 		if (rate < 0)
    469 			return EINVAL;
    470 		if (mode == AUMODE_PLAY) {
    471 			sc->sc_current.prate = rate;
    472 		} else {
    473 			sc->sc_current.rrate = rate;
    474 		}
    475 	}
    476 
    477 	return 0;
    478 }
    479 
    480 static void
    481 vs_set_sr(struct vs_softc *sc, int rate)
    482 {
    483 
    484 	DPRINTF(1, ("setting sample rate to %d, %d\n",
    485 		 rate, (int)vs_l2r[rate].rate));
    486 	bus_space_write_1(sc->sc_iot, sc->sc_ppi, PPI_PORTC,
    487 			  (bus_space_read_1 (sc->sc_iot, sc->sc_ppi,
    488 					     PPI_PORTC) & 0xf0)
    489 			  | vs_l2r[rate].den);
    490 	adpcm_chgclk(vs_l2r[rate].clk);
    491 }
    492 
    493 static inline void
    494 vs_set_po(struct vs_softc *sc, u_long po)
    495 {
    496 	bus_space_write_1(sc->sc_iot, sc->sc_ppi, PPI_PORTC,
    497 			  (bus_space_read_1(sc->sc_iot, sc->sc_ppi, PPI_PORTC)
    498 			   & 0xfc) | po);
    499 }
    500 
    501 static int
    502 vs_trigger_output(void *hdl, void *start, void *end, int bsize,
    503 		  void (*intr)(void *), void *arg,
    504 		  const audio_params_t *p)
    505 {
    506 	struct vs_softc *sc;
    507 	struct vs_dma *vd;
    508 	struct dmac_dma_xfer *xf;
    509 	struct dmac_channel_stat *chan;
    510 
    511 	DPRINTF(2, ("vs_trigger_output: start=%p, bsize=%d, intr=%p, arg=%p\n",
    512 		 start, bsize, intr, arg));
    513 	sc = hdl;
    514 	chan = sc->sc_dma_ch;
    515 	sc->sc_pintr = intr;
    516 	sc->sc_parg  = arg;
    517 	sc->sc_current.blksize = bsize;
    518 	sc->sc_current.bufsize = (char *)end - (char *)start;
    519 	sc->sc_current.dmap = 0;
    520 
    521 	/* Find DMA buffer. */
    522 	for (vd = sc->sc_dmas; vd != NULL && KVADDR(vd) != start;
    523 	     vd = vd->vd_next)
    524 		continue;
    525 	if (vd == NULL) {
    526 		printf("%s: trigger_output: bad addr %p\n",
    527 		    device_xname(sc->sc_dev), start);
    528 		return EINVAL;
    529 	}
    530 
    531 	vs_set_sr(sc, sc->sc_current.prate);
    532 	vs_set_po(sc, VS_PANOUT_LR);
    533 
    534 	xf = dmac_alloc_xfer(chan, sc->sc_dmat, vd->vd_map);
    535 	sc->sc_current.xfer = xf;
    536 	chan->ch_dcr = (DMAC_DCR_XRM_CSWOH | DMAC_DCR_OTYP_EASYNC |
    537 			DMAC_DCR_OPS_8BIT);
    538 	chan->ch_ocr = DMAC_OCR_REQG_EXTERNAL;
    539 	xf->dx_ocr = DMAC_OCR_DIR_MTD;
    540 	xf->dx_scr = DMAC_SCR_MAC_COUNT_UP | DMAC_SCR_DAC_NO_COUNT;
    541 	xf->dx_device = sc->sc_addr + MSM6258_DATA*2 + 1;
    542 
    543 	dmac_load_xfer(chan->ch_softc, xf);
    544 	dmac_start_xfer_offset(chan->ch_softc, xf, 0, sc->sc_current.blksize);
    545 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, MSM6258_STAT, 2);
    546 	sc->sc_active = 1;
    547 
    548 	return 0;
    549 }
    550 
    551 static int
    552 vs_trigger_input(void *hdl, void *start, void *end, int bsize,
    553 		 void (*intr)(void *), void *arg,
    554 		 const audio_params_t *p)
    555 {
    556 	struct vs_softc *sc;
    557 	struct vs_dma *vd;
    558 	struct dmac_dma_xfer *xf;
    559 	struct dmac_channel_stat *chan;
    560 
    561 	DPRINTF(2, ("vs_trigger_input: start=%p, bsize=%d, intr=%p, arg=%p\n",
    562 		 start, bsize, intr, arg));
    563 	sc = hdl;
    564 	chan = sc->sc_dma_ch;
    565 	sc->sc_rintr = intr;
    566 	sc->sc_rarg  = arg;
    567 	sc->sc_current.blksize = bsize;
    568 	sc->sc_current.bufsize = (char *)end - (char *)start;
    569 	sc->sc_current.dmap = 0;
    570 
    571 	/* Find DMA buffer. */
    572 	for (vd = sc->sc_dmas; vd != NULL && KVADDR(vd) != start;
    573 	     vd = vd->vd_next)
    574 		continue;
    575 	if (vd == NULL) {
    576 		printf("%s: trigger_output: bad addr %p\n",
    577 		    device_xname(sc->sc_dev), start);
    578 		return EINVAL;
    579 	}
    580 
    581 	vs_set_sr(sc, sc->sc_current.rrate);
    582 	xf = dmac_alloc_xfer(chan, sc->sc_dmat, vd->vd_map);
    583 	sc->sc_current.xfer = xf;
    584 	chan->ch_dcr = (DMAC_DCR_XRM_CSWOH | DMAC_DCR_OTYP_EASYNC |
    585 			DMAC_DCR_OPS_8BIT);
    586 	chan->ch_ocr = DMAC_OCR_REQG_EXTERNAL;
    587 	xf->dx_ocr = DMAC_OCR_DIR_DTM;
    588 	xf->dx_scr = DMAC_SCR_MAC_COUNT_UP | DMAC_SCR_DAC_NO_COUNT;
    589 	xf->dx_device = sc->sc_addr + MSM6258_DATA*2 + 1;
    590 
    591 	dmac_load_xfer(chan->ch_softc, xf);
    592 	dmac_start_xfer_offset(chan->ch_softc, xf, 0, sc->sc_current.blksize);
    593 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, MSM6258_STAT, 4);
    594 	sc->sc_active = 1;
    595 
    596 	return 0;
    597 }
    598 
    599 static int
    600 vs_halt_output(void *hdl)
    601 {
    602 	struct vs_softc *sc;
    603 
    604 	DPRINTF(1, ("vs_halt_output\n"));
    605 	sc = hdl;
    606 	if (sc->sc_active) {
    607 		/* stop ADPCM play */
    608 		dmac_abort_xfer(sc->sc_dma_ch->ch_softc, sc->sc_current.xfer);
    609 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, MSM6258_STAT, 1);
    610 		sc->sc_active = 0;
    611 	}
    612 
    613 	return 0;
    614 }
    615 
    616 static int
    617 vs_halt_input(void *hdl)
    618 {
    619 	struct vs_softc *sc;
    620 
    621 	DPRINTF(1, ("vs_halt_input\n"));
    622 	sc = hdl;
    623 	if (sc->sc_active) {
    624 		/* stop ADPCM recoding */
    625 		dmac_abort_xfer(sc->sc_dma_ch->ch_softc, sc->sc_current.xfer);
    626 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, MSM6258_STAT, 1);
    627 		sc->sc_active = 0;
    628 	}
    629 
    630 	return 0;
    631 }
    632 
    633 static int
    634 vs_allocmem(struct vs_softc *sc, size_t size, size_t align, size_t boundary,
    635 	struct vs_dma *vd)
    636 {
    637 	int error;
    638 
    639 #ifdef DIAGNOSTIC
    640 	if (size > DMAC_MAXSEGSZ)
    641 		panic ("vs_allocmem: maximum size exceeded, %d", (int) size);
    642 #endif
    643 
    644 	vd->vd_size = size;
    645 
    646 	error = bus_dmamem_alloc(vd->vd_dmat, vd->vd_size, align, boundary,
    647 				 vd->vd_segs,
    648 				 sizeof (vd->vd_segs) / sizeof (vd->vd_segs[0]),
    649 				 &vd->vd_nsegs, BUS_DMA_WAITOK);
    650 	if (error)
    651 		goto out;
    652 
    653 	error = bus_dmamem_map(vd->vd_dmat, vd->vd_segs, vd->vd_nsegs,
    654 			       vd->vd_size, &vd->vd_addr,
    655 			       BUS_DMA_WAITOK | BUS_DMA_COHERENT);
    656 	if (error)
    657 		goto free;
    658 
    659 	error = bus_dmamap_create(vd->vd_dmat, vd->vd_size, 1, DMAC_MAXSEGSZ,
    660 				  0, BUS_DMA_WAITOK, &vd->vd_map);
    661 	if (error)
    662 		goto unmap;
    663 
    664 	error = bus_dmamap_load(vd->vd_dmat, vd->vd_map, vd->vd_addr,
    665 				vd->vd_size, NULL, BUS_DMA_WAITOK);
    666 	if (error)
    667 		goto destroy;
    668 
    669 	return 0;
    670 
    671  destroy:
    672 	bus_dmamap_destroy(vd->vd_dmat, vd->vd_map);
    673  unmap:
    674 	bus_dmamem_unmap(vd->vd_dmat, vd->vd_addr, vd->vd_size);
    675  free:
    676 	bus_dmamem_free(vd->vd_dmat, vd->vd_segs, vd->vd_nsegs);
    677  out:
    678 	return error;
    679 }
    680 
    681 static void
    682 vs_freemem(struct vs_dma *vd)
    683 {
    684 
    685 	bus_dmamap_unload(vd->vd_dmat, vd->vd_map);
    686 	bus_dmamap_destroy(vd->vd_dmat, vd->vd_map);
    687 	bus_dmamem_unmap(vd->vd_dmat, vd->vd_addr, vd->vd_size);
    688 	bus_dmamem_free(vd->vd_dmat, vd->vd_segs, vd->vd_nsegs);
    689 }
    690 
    691 static int
    692 vs_getdev(void *hdl, struct audio_device *retp)
    693 {
    694 
    695 	DPRINTF(1, ("vs_getdev\n"));
    696 	*retp = vs_device;
    697 	return 0;
    698 }
    699 
    700 static int
    701 vs_set_port(void *hdl, mixer_ctrl_t *cp)
    702 {
    703 
    704 	DPRINTF(1, ("vs_set_port\n"));
    705 	return 0;
    706 }
    707 
    708 static int
    709 vs_get_port(void *hdl, mixer_ctrl_t *cp)
    710 {
    711 
    712 	DPRINTF(1, ("vs_get_port\n"));
    713 	return 0;
    714 }
    715 
    716 static int
    717 vs_query_devinfo(void *hdl, mixer_devinfo_t *mi)
    718 {
    719 
    720 	DPRINTF(1, ("vs_query_devinfo\n"));
    721 	switch (mi->index) {
    722 	default:
    723 		return EINVAL;
    724 	}
    725 	return 0;
    726 }
    727 
    728 static void *
    729 vs_allocm(void *hdl, int direction, size_t size)
    730 {
    731 	struct vs_softc *sc;
    732 	struct vs_dma *vd;
    733 	int error;
    734 
    735 	vd = kmem_alloc(sizeof(*vd), KM_SLEEP);
    736 	sc = hdl;
    737 	vd->vd_dmat = sc->sc_dmat;
    738 
    739 	error = vs_allocmem(sc, size, 32, 0, vd);
    740 	if (error) {
    741 		kmem_free(vd, sizeof(*vd));
    742 		return NULL;
    743 	}
    744 	vd->vd_next = sc->sc_dmas;
    745 	sc->sc_dmas = vd;
    746 
    747 	return KVADDR(vd);
    748 }
    749 
    750 static void
    751 vs_freem(void *hdl, void *addr, size_t size)
    752 {
    753 	struct vs_softc *sc;
    754 	struct vs_dma *p, **pp;
    755 
    756 	sc = hdl;
    757 	for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->vd_next) {
    758 		if (KVADDR(p) == addr) {
    759 			vs_freemem(p);
    760 			*pp = p->vd_next;
    761 			kmem_free(p, sizeof(*p));
    762 			return;
    763 		}
    764 	}
    765 }
    766 
    767 static size_t
    768 vs_round_buffersize(void *hdl, int direction, size_t bufsize)
    769 {
    770 
    771 	if (bufsize > DMAC_MAXSEGSZ)
    772 		bufsize = DMAC_MAXSEGSZ;
    773 	return bufsize;
    774 }
    775 
    776 #if 0
    777 paddr_t
    778 vs_mappage(void *addr, void *mem, off_t off, int prot)
    779 {
    780 	struct vs_softc *sc;
    781 	struct vs_dma *p;
    782 
    783 	if (off < 0)
    784 		return -1;
    785 	sc = addr;
    786 	for (p = sc->sc_dmas; p != NULL && KVADDR(p) != mem;
    787 	     p = p->vd_next)
    788 		continue;
    789 	if (p == NULL) {
    790 		printf("%s: mappage: bad addr %p\n",
    791 		    device_xname(sc->sc_dev), start);
    792 		return -1;
    793 	}
    794 
    795 	return bus_dmamem_mmap(sc->sc_dmat, p->vd_segs, p->vd_nsegs,
    796 			       off, prot, BUS_DMA_WAITOK);
    797 }
    798 #endif
    799 
    800 static int
    801 vs_get_props(void *hdl)
    802 {
    803 
    804 	DPRINTF(1, ("vs_get_props\n"));
    805 	return 0 /* | dependent | half duplex | no mmap */;
    806 }
    807 
    808 static void
    809 vs_get_locks(void *hdl, kmutex_t **intr, kmutex_t **thread)
    810 {
    811 	struct vs_softc *sc;
    812 
    813 	DPRINTF(1, ("vs_get_locks\n"));
    814 	sc = hdl;
    815 	*intr = &sc->sc_intr_lock;
    816 	*thread = &sc->sc_lock;
    817 }
    818 
    819 #endif /* NAUDIO > 0 && NVS > 0*/
    820