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