Home | History | Annotate | Line # | Download | only in dev
vs.c revision 1.39
      1 /*	$NetBSD: vs.c,v 1.39 2017/07/09 12:49:26 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.39 2017/07/09 12:49:26 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 static int
    164 vs_match(device_t parent, cfdata_t cf, void *aux)
    165 {
    166 	struct intio_attach_args *ia;
    167 
    168 	ia = aux;
    169 	if (strcmp(ia->ia_name, "vs") || vs_attached)
    170 		return 0;
    171 
    172 	if (ia->ia_addr == INTIOCF_ADDR_DEFAULT)
    173 		ia->ia_addr = VS_ADDR;
    174 	if (ia->ia_dma == INTIOCF_DMA_DEFAULT)
    175 		ia->ia_dma = VS_DMA;
    176 	if (ia->ia_dmaintr == INTIOCF_DMAINTR_DEFAULT)
    177 		ia->ia_dmaintr = VS_DMAINTR;
    178 
    179 	/* fixed parameters */
    180 	if (ia->ia_addr != VS_ADDR)
    181 		return 0;
    182 	if (ia->ia_dma != VS_DMA)
    183 		return 0;
    184 	if (ia->ia_dmaintr != VS_DMAINTR)
    185 		return 0;
    186 
    187 #ifdef VS_DEBUG
    188 	vs_debug = 1;
    189 #ifdef AUDIO_DEBUG
    190 	audiodebug = 2;
    191 #endif
    192 #endif
    193 
    194 	return 1;
    195 }
    196 
    197 static void
    198 vs_attach(device_t parent, device_t self, void *aux)
    199 {
    200 	struct vs_softc *sc;
    201 	bus_space_tag_t iot;
    202 	bus_space_handle_t ioh;
    203 	struct intio_attach_args *ia;
    204 
    205 	sc = device_private(self);
    206 	sc->sc_dev = self;
    207 	ia = aux;
    208 	vs_attached = 1;
    209 
    210 	printf("\n");
    211 
    212 	/* Re-map the I/O space */
    213 	iot = ia->ia_bst;
    214 	bus_space_map(iot, ia->ia_addr, 0x2000, BUS_SPACE_MAP_SHIFTED, &ioh);
    215 
    216 	/* Initialize sc */
    217 	sc->sc_iot = iot;
    218 	sc->sc_ioh = ioh;
    219 	sc->sc_hw_if = &vs_hw_if;
    220 	sc->sc_addr = (void *) ia->ia_addr;
    221 	sc->sc_dmas = NULL;
    222 	sc->sc_active = 0;
    223 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    224 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SCHED);
    225 
    226 	/* XXX */
    227 	bus_space_map(iot, PPI_ADDR, PPI_MAPSIZE, BUS_SPACE_MAP_SHIFTED,
    228 		      &sc->sc_ppi);
    229 
    230 	/* Initialize DMAC */
    231 	sc->sc_dmat = ia->ia_dmat;
    232 	sc->sc_dma_ch = dmac_alloc_channel(parent, ia->ia_dma, "vs",
    233 		ia->ia_dmaintr,   vs_dmaintr, sc,
    234 		ia->ia_dmaintr+1, vs_dmaerrintr, sc);
    235 
    236 	aprint_normal_dev(self, "MSM6258V ADPCM voice synthesizer\n");
    237 
    238 	audio_attach_mi(&vs_hw_if, sc, sc->sc_dev);
    239 }
    240 
    241 /*
    242  * vs interrupt handler
    243  */
    244 static int
    245 vs_dmaintr(void *hdl)
    246 {
    247 	struct vs_softc *sc;
    248 
    249 	DPRINTF(2, ("vs_dmaintr\n"));
    250 	sc = hdl;
    251 
    252 	mutex_spin_enter(&sc->sc_intr_lock);
    253 
    254 	if (sc->sc_pintr) {
    255 		/* start next transfer */
    256 		sc->sc_current.dmap += sc->sc_current.blksize;
    257 		if (sc->sc_current.dmap + sc->sc_current.blksize
    258 		    > sc->sc_current.bufsize)
    259 			sc->sc_current.dmap -= sc->sc_current.bufsize;
    260 		dmac_start_xfer_offset(sc->sc_dma_ch->ch_softc,
    261 					sc->sc_current.xfer,
    262 					sc->sc_current.dmap,
    263 					sc->sc_current.blksize);
    264 		sc->sc_pintr(sc->sc_parg);
    265 	} else if (sc->sc_rintr) {
    266 		/* start next transfer */
    267 		sc->sc_current.dmap += sc->sc_current.blksize;
    268 		if (sc->sc_current.dmap + sc->sc_current.blksize
    269 		    > sc->sc_current.bufsize)
    270 			sc->sc_current.dmap -= sc->sc_current.bufsize;
    271 		dmac_start_xfer_offset(sc->sc_dma_ch->ch_softc,
    272 					sc->sc_current.xfer,
    273 					sc->sc_current.dmap,
    274 					sc->sc_current.blksize);
    275 		sc->sc_rintr(sc->sc_rarg);
    276 	} else {
    277 		printf("vs_dmaintr: spurious interrupt\n");
    278 	}
    279 
    280 	mutex_spin_exit(&sc->sc_intr_lock);
    281 
    282 	return 1;
    283 }
    284 
    285 static int
    286 vs_dmaerrintr(void *hdl)
    287 {
    288 	struct vs_softc *sc;
    289 
    290 	sc = hdl;
    291 	DPRINTF(1, ("%s: DMA transfer error.\n", device_xname(sc->sc_dev)));
    292 	/* XXX */
    293 	vs_dmaintr(sc);
    294 
    295 	return 1;
    296 }
    297 
    298 
    299 /*
    300  * audio MD layer interfaces
    301  */
    302 
    303 static int
    304 vs_open(void *hdl, int flags)
    305 {
    306 	struct vs_softc *sc;
    307 
    308 	DPRINTF(1, ("vs_open: flags=%d\n", flags));
    309 	sc = hdl;
    310 	sc->sc_pintr = NULL;
    311 	sc->sc_rintr = NULL;
    312 
    313 	return 0;
    314 }
    315 
    316 static void
    317 vs_close(void *hdl)
    318 {
    319 
    320 	DPRINTF(1, ("vs_close\n"));
    321 }
    322 
    323 static int
    324 vs_query_encoding(void *hdl, struct audio_encoding *fp)
    325 {
    326 
    327 	DPRINTF(1, ("vs_query_encoding\n"));
    328 
    329 	if (fp->index == 0) {
    330 		strcpy(fp->name, AudioEslinear_be);
    331 		fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
    332 		fp->precision = 16;
    333 		fp->flags = 0;
    334 		return 0;
    335 	}
    336 	return EINVAL;
    337 }
    338 
    339 static int
    340 vs_round_sr(u_long rate)
    341 {
    342 	int i;
    343 	int diff;
    344 	int nearest;
    345 
    346 	diff = rate;
    347 	nearest = 0;
    348 	for (i = 0; i < NUM_RATE; i++) {
    349 		if (rate >= vs_l2r[i].rate) {
    350 			if (rate - vs_l2r[i].rate < diff) {
    351 				diff = rate - vs_l2r[i].rate;
    352 				nearest = i;
    353 			}
    354 		} else {
    355 			if (vs_l2r[i].rate - rate < diff) {
    356 				diff = vs_l2r[i].rate - rate;
    357 				nearest = i;
    358 			}
    359 		}
    360 	}
    361 	if (diff * 100 / rate > 15)
    362 		return -1;
    363 	else
    364 		return nearest;
    365 }
    366 
    367 static int
    368 vs_set_params(void *hdl, int setmode, int usemode,
    369 	audio_params_t *play, audio_params_t *rec,
    370 	stream_filter_list_t *pfil, stream_filter_list_t *rfil)
    371 {
    372 	struct vs_softc *sc;
    373 	int rate;
    374 
    375 	sc = hdl;
    376 
    377 	DPRINTF(1, ("vs_set_params: mode=%d enc=%d rate=%d prec=%d ch=%d: ",
    378 		setmode, play->encoding, play->sample_rate,
    379 		play->precision, play->channels));
    380 
    381 	if (play->channels != 1) {
    382 		DPRINTF(1, ("channels not matched\n"));
    383 		return EINVAL;
    384 	}
    385 
    386 	rate = vs_round_sr(play->sample_rate);
    387 	if (rate < 0) {
    388 		DPRINTF(1, ("rate not matched\n"));
    389 		return EINVAL;
    390 	}
    391 
    392 	if (play->encoding != AUDIO_ENCODING_SLINEAR_BE) {
    393 		DPRINTF(1, ("encoding not matched\n"));
    394 		return EINVAL;
    395 	}
    396 	if (play->precision != 16) {
    397 		DPRINTF(1, ("precision not matched\n"));
    398 		return EINVAL;
    399 	}
    400 
    401 	play->encoding = AUDIO_ENCODING_ADPCM;
    402 	play->validbits = 4;
    403 	play->precision = 4;
    404 
    405 	pfil->prepend(pfil, msm6258_slinear16_to_adpcm, play);
    406 	rfil->prepend(rfil, msm6258_adpcm_to_slinear16, play);
    407 
    408 	sc->sc_current.prate = rate;
    409 	sc->sc_current.rrate = rate;
    410 
    411 	/* copy to rec because it's !AUDIO_PROP_INDEPENDENT */
    412 	*rec = *play;
    413 
    414 	DPRINTF(1, ("accepted\n"));
    415 	return 0;
    416 }
    417 
    418 static void
    419 vs_set_sr(struct vs_softc *sc, int rate)
    420 {
    421 
    422 	DPRINTF(1, ("setting sample rate to %d, %d\n",
    423 		 rate, (int)vs_l2r[rate].rate));
    424 	bus_space_write_1(sc->sc_iot, sc->sc_ppi, PPI_PORTC,
    425 			  (bus_space_read_1 (sc->sc_iot, sc->sc_ppi,
    426 					     PPI_PORTC) & 0xf0)
    427 			  | vs_l2r[rate].den);
    428 	adpcm_chgclk(vs_l2r[rate].clk);
    429 }
    430 
    431 static inline void
    432 vs_set_po(struct vs_softc *sc, u_long po)
    433 {
    434 	bus_space_write_1(sc->sc_iot, sc->sc_ppi, PPI_PORTC,
    435 			  (bus_space_read_1(sc->sc_iot, sc->sc_ppi, PPI_PORTC)
    436 			   & 0xfc) | po);
    437 }
    438 
    439 static int
    440 vs_trigger_output(void *hdl, void *start, void *end, int bsize,
    441 		  void (*intr)(void *), void *arg,
    442 		  const audio_params_t *p)
    443 {
    444 	struct vs_softc *sc;
    445 	struct vs_dma *vd;
    446 	struct dmac_dma_xfer *xf;
    447 	struct dmac_channel_stat *chan;
    448 
    449 	DPRINTF(2, ("vs_trigger_output: start=%p, bsize=%d, intr=%p, arg=%p\n",
    450 		 start, bsize, intr, arg));
    451 	sc = hdl;
    452 	chan = sc->sc_dma_ch;
    453 	sc->sc_pintr = intr;
    454 	sc->sc_parg  = arg;
    455 	sc->sc_current.blksize = bsize;
    456 	sc->sc_current.bufsize = (char *)end - (char *)start;
    457 	sc->sc_current.dmap = 0;
    458 
    459 	/* Find DMA buffer. */
    460 	for (vd = sc->sc_dmas; vd != NULL && KVADDR(vd) != start;
    461 	     vd = vd->vd_next)
    462 		continue;
    463 	if (vd == NULL) {
    464 		printf("%s: trigger_output: bad addr %p\n",
    465 		    device_xname(sc->sc_dev), start);
    466 		return EINVAL;
    467 	}
    468 
    469 	vs_set_sr(sc, sc->sc_current.prate);
    470 	vs_set_po(sc, VS_PANOUT_LR);
    471 
    472 	xf = dmac_alloc_xfer(chan, sc->sc_dmat, vd->vd_map);
    473 	sc->sc_current.xfer = xf;
    474 	chan->ch_dcr = (DMAC_DCR_XRM_CSWOH | DMAC_DCR_OTYP_EASYNC |
    475 			DMAC_DCR_OPS_8BIT);
    476 	chan->ch_ocr = DMAC_OCR_REQG_EXTERNAL;
    477 	xf->dx_ocr = DMAC_OCR_DIR_MTD;
    478 	xf->dx_scr = DMAC_SCR_MAC_COUNT_UP | DMAC_SCR_DAC_NO_COUNT;
    479 	xf->dx_device = sc->sc_addr + MSM6258_DATA*2 + 1;
    480 
    481 	dmac_load_xfer(chan->ch_softc, xf);
    482 	dmac_start_xfer_offset(chan->ch_softc, xf, 0, sc->sc_current.blksize);
    483 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, MSM6258_STAT, 2);
    484 	sc->sc_active = 1;
    485 
    486 	return 0;
    487 }
    488 
    489 static int
    490 vs_trigger_input(void *hdl, void *start, void *end, int bsize,
    491 		 void (*intr)(void *), void *arg,
    492 		 const audio_params_t *p)
    493 {
    494 	struct vs_softc *sc;
    495 	struct vs_dma *vd;
    496 	struct dmac_dma_xfer *xf;
    497 	struct dmac_channel_stat *chan;
    498 
    499 	DPRINTF(2, ("vs_trigger_input: start=%p, bsize=%d, intr=%p, arg=%p\n",
    500 		 start, bsize, intr, arg));
    501 	sc = hdl;
    502 	chan = sc->sc_dma_ch;
    503 	sc->sc_rintr = intr;
    504 	sc->sc_rarg  = arg;
    505 	sc->sc_current.blksize = bsize;
    506 	sc->sc_current.bufsize = (char *)end - (char *)start;
    507 	sc->sc_current.dmap = 0;
    508 
    509 	/* Find DMA buffer. */
    510 	for (vd = sc->sc_dmas; vd != NULL && KVADDR(vd) != start;
    511 	     vd = vd->vd_next)
    512 		continue;
    513 	if (vd == NULL) {
    514 		printf("%s: trigger_output: bad addr %p\n",
    515 		    device_xname(sc->sc_dev), start);
    516 		return EINVAL;
    517 	}
    518 
    519 	vs_set_sr(sc, sc->sc_current.rrate);
    520 	xf = dmac_alloc_xfer(chan, sc->sc_dmat, vd->vd_map);
    521 	sc->sc_current.xfer = xf;
    522 	chan->ch_dcr = (DMAC_DCR_XRM_CSWOH | DMAC_DCR_OTYP_EASYNC |
    523 			DMAC_DCR_OPS_8BIT);
    524 	chan->ch_ocr = DMAC_OCR_REQG_EXTERNAL;
    525 	xf->dx_ocr = DMAC_OCR_DIR_DTM;
    526 	xf->dx_scr = DMAC_SCR_MAC_COUNT_UP | DMAC_SCR_DAC_NO_COUNT;
    527 	xf->dx_device = sc->sc_addr + MSM6258_DATA*2 + 1;
    528 
    529 	dmac_load_xfer(chan->ch_softc, xf);
    530 	dmac_start_xfer_offset(chan->ch_softc, xf, 0, sc->sc_current.blksize);
    531 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, MSM6258_STAT, 4);
    532 	sc->sc_active = 1;
    533 
    534 	return 0;
    535 }
    536 
    537 static int
    538 vs_halt_output(void *hdl)
    539 {
    540 	struct vs_softc *sc;
    541 
    542 	DPRINTF(1, ("vs_halt_output\n"));
    543 	sc = hdl;
    544 	if (sc->sc_active) {
    545 		/* stop ADPCM play */
    546 		dmac_abort_xfer(sc->sc_dma_ch->ch_softc, sc->sc_current.xfer);
    547 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, MSM6258_STAT, 1);
    548 		sc->sc_active = 0;
    549 	}
    550 
    551 	return 0;
    552 }
    553 
    554 static int
    555 vs_halt_input(void *hdl)
    556 {
    557 	struct vs_softc *sc;
    558 
    559 	DPRINTF(1, ("vs_halt_input\n"));
    560 	sc = hdl;
    561 	if (sc->sc_active) {
    562 		/* stop ADPCM recoding */
    563 		dmac_abort_xfer(sc->sc_dma_ch->ch_softc, sc->sc_current.xfer);
    564 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, MSM6258_STAT, 1);
    565 		sc->sc_active = 0;
    566 	}
    567 
    568 	return 0;
    569 }
    570 
    571 static int
    572 vs_allocmem(struct vs_softc *sc, size_t size, size_t align, size_t boundary,
    573 	struct vs_dma *vd)
    574 {
    575 	int error;
    576 
    577 #ifdef DIAGNOSTIC
    578 	if (size > DMAC_MAXSEGSZ)
    579 		panic ("vs_allocmem: maximum size exceeded, %d", (int) size);
    580 #endif
    581 
    582 	vd->vd_size = size;
    583 
    584 	error = bus_dmamem_alloc(vd->vd_dmat, vd->vd_size, align, boundary,
    585 				 vd->vd_segs,
    586 				 sizeof (vd->vd_segs) / sizeof (vd->vd_segs[0]),
    587 				 &vd->vd_nsegs, BUS_DMA_WAITOK);
    588 	if (error)
    589 		goto out;
    590 
    591 	error = bus_dmamem_map(vd->vd_dmat, vd->vd_segs, vd->vd_nsegs,
    592 			       vd->vd_size, &vd->vd_addr,
    593 			       BUS_DMA_WAITOK | BUS_DMA_COHERENT);
    594 	if (error)
    595 		goto free;
    596 
    597 	error = bus_dmamap_create(vd->vd_dmat, vd->vd_size, 1, DMAC_MAXSEGSZ,
    598 				  0, BUS_DMA_WAITOK, &vd->vd_map);
    599 	if (error)
    600 		goto unmap;
    601 
    602 	error = bus_dmamap_load(vd->vd_dmat, vd->vd_map, vd->vd_addr,
    603 				vd->vd_size, NULL, BUS_DMA_WAITOK);
    604 	if (error)
    605 		goto destroy;
    606 
    607 	return 0;
    608 
    609  destroy:
    610 	bus_dmamap_destroy(vd->vd_dmat, vd->vd_map);
    611  unmap:
    612 	bus_dmamem_unmap(vd->vd_dmat, vd->vd_addr, vd->vd_size);
    613  free:
    614 	bus_dmamem_free(vd->vd_dmat, vd->vd_segs, vd->vd_nsegs);
    615  out:
    616 	return error;
    617 }
    618 
    619 static void
    620 vs_freemem(struct vs_dma *vd)
    621 {
    622 
    623 	bus_dmamap_unload(vd->vd_dmat, vd->vd_map);
    624 	bus_dmamap_destroy(vd->vd_dmat, vd->vd_map);
    625 	bus_dmamem_unmap(vd->vd_dmat, vd->vd_addr, vd->vd_size);
    626 	bus_dmamem_free(vd->vd_dmat, vd->vd_segs, vd->vd_nsegs);
    627 }
    628 
    629 static int
    630 vs_getdev(void *hdl, struct audio_device *retp)
    631 {
    632 
    633 	DPRINTF(1, ("vs_getdev\n"));
    634 	*retp = vs_device;
    635 	return 0;
    636 }
    637 
    638 static int
    639 vs_set_port(void *hdl, mixer_ctrl_t *cp)
    640 {
    641 
    642 	DPRINTF(1, ("vs_set_port\n"));
    643 	return 0;
    644 }
    645 
    646 static int
    647 vs_get_port(void *hdl, mixer_ctrl_t *cp)
    648 {
    649 
    650 	DPRINTF(1, ("vs_get_port\n"));
    651 	return 0;
    652 }
    653 
    654 static int
    655 vs_query_devinfo(void *hdl, mixer_devinfo_t *mi)
    656 {
    657 
    658 	DPRINTF(1, ("vs_query_devinfo\n"));
    659 	switch (mi->index) {
    660 	default:
    661 		return EINVAL;
    662 	}
    663 	return 0;
    664 }
    665 
    666 static void *
    667 vs_allocm(void *hdl, int direction, size_t size)
    668 {
    669 	struct vs_softc *sc;
    670 	struct vs_dma *vd;
    671 	int error;
    672 
    673 	vd = kmem_alloc(sizeof(*vd), KM_SLEEP);
    674 	sc = hdl;
    675 	vd->vd_dmat = sc->sc_dmat;
    676 
    677 	error = vs_allocmem(sc, size, 32, 0, vd);
    678 	if (error) {
    679 		kmem_free(vd, sizeof(*vd));
    680 		return NULL;
    681 	}
    682 	vd->vd_next = sc->sc_dmas;
    683 	sc->sc_dmas = vd;
    684 
    685 	return KVADDR(vd);
    686 }
    687 
    688 static void
    689 vs_freem(void *hdl, void *addr, size_t size)
    690 {
    691 	struct vs_softc *sc;
    692 	struct vs_dma *p, **pp;
    693 
    694 	sc = hdl;
    695 	for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->vd_next) {
    696 		if (KVADDR(p) == addr) {
    697 			vs_freemem(p);
    698 			*pp = p->vd_next;
    699 			kmem_free(p, sizeof(*p));
    700 			return;
    701 		}
    702 	}
    703 }
    704 
    705 static size_t
    706 vs_round_buffersize(void *hdl, int direction, size_t bufsize)
    707 {
    708 
    709 	if (bufsize > DMAC_MAXSEGSZ)
    710 		bufsize = DMAC_MAXSEGSZ;
    711 	return bufsize;
    712 }
    713 
    714 #if 0
    715 paddr_t
    716 vs_mappage(void *addr, void *mem, off_t off, int prot)
    717 {
    718 	struct vs_softc *sc;
    719 	struct vs_dma *p;
    720 
    721 	if (off < 0)
    722 		return -1;
    723 	sc = addr;
    724 	for (p = sc->sc_dmas; p != NULL && KVADDR(p) != mem;
    725 	     p = p->vd_next)
    726 		continue;
    727 	if (p == NULL) {
    728 		printf("%s: mappage: bad addr %p\n",
    729 		    device_xname(sc->sc_dev), start);
    730 		return -1;
    731 	}
    732 
    733 	return bus_dmamem_mmap(sc->sc_dmat, p->vd_segs, p->vd_nsegs,
    734 			       off, prot, BUS_DMA_WAITOK);
    735 }
    736 #endif
    737 
    738 static int
    739 vs_get_props(void *hdl)
    740 {
    741 
    742 	DPRINTF(1, ("vs_get_props\n"));
    743 	return 0 /* | dependent | half duplex | no mmap */;
    744 }
    745 
    746 static void
    747 vs_get_locks(void *hdl, kmutex_t **intr, kmutex_t **thread)
    748 {
    749 	struct vs_softc *sc;
    750 
    751 	DPRINTF(1, ("vs_get_locks\n"));
    752 	sc = hdl;
    753 	*intr = &sc->sc_intr_lock;
    754 	*thread = &sc->sc_lock;
    755 }
    756 
    757 #endif /* NAUDIO > 0 && NVS > 0*/
    758