Home | History | Annotate | Line # | Download | only in dev
vs.c revision 1.12
      1 /*	$NetBSD: vs.c,v 1.12 2002/03/16 09:00:43 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  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *      This product includes software developed by Tetsuya Isaki.
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  * SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * VS - OKI MSM6258 ADPCM voice synthesizer device driver.
     35  */
     36 
     37 #include "audio.h"
     38 #include "vs.h"
     39 #if NAUDIO > 0 && NVS > 0
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/device.h>
     44 
     45 #include <sys/audioio.h>
     46 #include <dev/audio_if.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/opmreg.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 __P((struct device *, struct cfdata *, void *));
     70 static void vs_attach __P((struct device *, struct device *, void *));
     71 
     72 static int  vs_dmaintr __P((void *));
     73 static int  vs_dmaerrintr __P((void *));
     74 
     75 /* MI audio layer interface */
     76 static int  vs_open __P((void *, int));
     77 static void vs_close __P((void *));
     78 static int  vs_query_encoding __P((void *, struct audio_encoding *));
     79 static int  vs_set_params __P((void *, int, int, struct audio_params *,
     80 	struct audio_params *));
     81 static int  vs_trigger_output __P((void *, void *, void *, int,
     82 				   void (*)(void *), void *,
     83 				   struct audio_params *));
     84 static int  vs_trigger_input __P((void *, void *, void *, int,
     85 				   void (*)(void *), void *,
     86 				   struct audio_params *));
     87 static int  vs_halt_output __P((void *));
     88 static int  vs_halt_input __P((void *));
     89 static int  vs_allocmem __P((struct vs_softc *, size_t, size_t, size_t, int,
     90 			    struct vs_dma *));
     91 static void vs_freemem __P((struct vs_dma *));
     92 static int  vs_getdev __P((void *, struct audio_device *));
     93 static int  vs_set_port __P((void *, mixer_ctrl_t *));
     94 static int  vs_get_port __P((void *, mixer_ctrl_t *));
     95 static int  vs_query_devinfo __P((void *, mixer_devinfo_t *));
     96 static void *vs_allocm __P((void *, int, size_t, int, int));
     97 static void vs_freem __P((void *, void *, int));
     98 static size_t vs_round_buffersize __P((void *, int, size_t));
     99 static int  vs_get_props __P((void *));
    100 
    101 /* lower functions */
    102 static int vs_round_sr(u_long);
    103 static void vs_set_sr(struct vs_softc *sc, int);
    104 static inline void vs_set_po(struct vs_softc *sc, u_long);
    105 
    106 extern struct cfdata vs_cd;
    107 
    108 struct cfattach vs_ca = {
    109 	sizeof(struct vs_softc), vs_match, vs_attach
    110 };
    111 
    112 static struct audio_hw_if vs_hw_if = {
    113 	vs_open,
    114 	vs_close,
    115 	NULL,			/* drain */
    116 
    117 	vs_query_encoding,
    118 	vs_set_params,
    119 	NULL,			/* round_blocksize */
    120 	NULL,			/* commit_settings */
    121 
    122 	NULL, 			/* init_output */
    123 	NULL, 			/* init_input */
    124 	NULL, 			/* start_output */
    125 	NULL,			/* start_input */
    126 
    127 	vs_halt_output,
    128 	vs_halt_input,
    129 	NULL,			/* speaker_ctl */
    130 
    131 	vs_getdev,
    132 	NULL,			/* setfd */
    133 
    134 	vs_set_port,
    135 	vs_get_port,
    136 	vs_query_devinfo,
    137 
    138 	vs_allocm,
    139 	vs_freem,
    140 	vs_round_buffersize,
    141 	NULL,			/* mappage */
    142 
    143 	vs_get_props,
    144 
    145 	vs_trigger_output,
    146 	vs_trigger_input,
    147 
    148 	NULL,
    149 };
    150 
    151 static struct audio_device vs_device = {
    152 	"OKI MSM6258",
    153 	"",
    154 	"vs"
    155 };
    156 
    157 struct {
    158 	u_long rate;
    159 	u_char clk;
    160 	u_char den;
    161 } vs_l2r[] = {
    162 	{ VS_RATE_15K, VS_CLK_8MHZ, VS_SRATE_512 },
    163 	{ VS_RATE_10K, VS_CLK_8MHZ, VS_SRATE_768 },
    164 	{ VS_RATE_7K,  VS_CLK_8MHZ, VS_SRATE_1024},
    165 	{ VS_RATE_5K,  VS_CLK_4MHZ, VS_SRATE_768 },
    166 	{ VS_RATE_3K,  VS_CLK_4MHZ, VS_SRATE_1024}
    167 };
    168 
    169 #define NUM_RATE	(sizeof(vs_l2r)/sizeof(vs_l2r[0]))
    170 
    171 struct audio_encoding vs_encodings[] = {
    172 	{0, AudioEadpcm, AUDIO_ENCODING_ADPCM, 4, 0},
    173 	{1, AudioEulinear, AUDIO_ENCODING_ULINEAR, 8,
    174 	    AUDIO_ENCODINGFLAG_EMULATED},
    175 	{2, AudioEmulaw, AUDIO_ENCODING_ULAW, 8, AUDIO_ENCODINGFLAG_EMULATED},
    176 };
    177 
    178 static int
    179 vs_match(struct device *parent, struct cfdata *cf, void *aux)
    180 {
    181 	struct intio_attach_args *ia = aux;
    182 
    183 	if (strcmp(ia->ia_name, "vs") || cf->cf_unit > 0)
    184 		return 0;
    185 
    186 	if (ia->ia_addr == INTIOCF_ADDR_DEFAULT)
    187 		ia->ia_addr = VS_ADDR;
    188 	if (ia->ia_dma == INTIOCF_DMA_DEFAULT)
    189 		ia->ia_dma = VS_DMA;
    190 	if (ia->ia_dmaintr == INTIOCF_DMAINTR_DEFAULT)
    191 		ia->ia_dmaintr = VS_DMAINTR;
    192 
    193 	/* fixed parameters */
    194 	if (ia->ia_addr != VS_ADDR)
    195 		return 0;
    196 	if (ia->ia_dma != VS_DMA)
    197 		return 0;
    198 	if (ia->ia_dmaintr != VS_DMAINTR)
    199 		return 0;
    200 
    201 #ifdef VS_DEBUG
    202 	vs_debug = 1;
    203 #ifdef AUDIO_DEBUG
    204 	audiodebug = 2;
    205 #endif
    206 #endif
    207 
    208 	return 1;
    209 }
    210 
    211 static void
    212 vs_attach(struct device *parent, struct device *self, void *aux)
    213 {
    214 	struct vs_softc *sc = (struct vs_softc *)self;
    215 	bus_space_tag_t iot;
    216 	bus_space_handle_t ioh;
    217 	struct intio_attach_args *ia = aux;
    218 
    219 	printf("\n");
    220 
    221 	/* Re-map the I/O space */
    222 	iot = ia->ia_bst;
    223 	bus_space_map(iot, ia->ia_addr, 0x2000, BUS_SPACE_MAP_SHIFTED, &ioh);
    224 
    225 	/* Initialize sc */
    226 	sc->sc_iot = iot;
    227 	sc->sc_ioh = ioh;
    228 	sc->sc_hw_if = &vs_hw_if;
    229 	sc->sc_addr = (caddr_t) ia->ia_addr;
    230 	sc->sc_dmas = NULL;
    231 
    232 	/* Initialize codec */
    233 	sc->sc_codec = msm6258_codec_init();
    234 	if (sc->sc_codec == NULL) {
    235 		printf ("Could not init codec\n");
    236 		return;
    237 	}
    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 	printf("%s: MSM6258V ADPCM voice synthesizer\n", sc->sc_dev.dv_xname);
    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 = hdl;
    261 
    262 	DPRINTF(2, ("vs_dmaintr\n"));
    263 
    264 	if (sc->sc_pintr) {
    265 		/* start next transfer */
    266 		sc->sc_current.dmap += sc->sc_current.blksize;
    267 		if (sc->sc_current.dmap + sc->sc_current.blksize
    268 			> sc->sc_current.bufsize)
    269 			sc->sc_current.dmap -= sc->sc_current.bufsize;
    270 		dmac_start_xfer_offset (sc->sc_dma_ch->ch_softc,
    271 					sc->sc_current.xfer,
    272 					sc->sc_current.dmap,
    273 					sc->sc_current.blksize);
    274 		sc->sc_pintr(sc->sc_parg);
    275 	} else if (sc->sc_rintr) {
    276 		/* start next transfer */
    277 		sc->sc_current.dmap += sc->sc_current.blksize;
    278 		if (sc->sc_current.dmap + sc->sc_current.blksize
    279 			> sc->sc_current.bufsize)
    280 			sc->sc_current.dmap -= sc->sc_current.bufsize;
    281 		dmac_start_xfer_offset (sc->sc_dma_ch->ch_softc,
    282 					sc->sc_current.xfer,
    283 					sc->sc_current.dmap,
    284 					sc->sc_current.blksize);
    285 		sc->sc_rintr(sc->sc_rarg);
    286 	} else {
    287 		printf ("vs_dmaintr: spurious interrupt\n");
    288 	}
    289 
    290 	return 1;
    291 }
    292 
    293 static int
    294 vs_dmaerrintr(void *hdl)
    295 {
    296 	struct vs_softc *sc = hdl;
    297 
    298 	DPRINTF(1, ("%s: DMA transfer error.\n", sc->sc_dev.dv_xname));
    299 	/* XXX */
    300 	vs_dmaintr(sc);
    301 
    302 	return 1;
    303 }
    304 
    305 
    306 /*
    307  * audio MD layer interfaces
    308  */
    309 
    310 static int
    311 vs_open(void *hdl, int flags)
    312 {
    313 	struct vs_softc *sc = hdl;
    314 
    315 	DPRINTF(1, ("vs_open: flags=%d\n", flags));
    316 
    317 	sc->sc_pintr = NULL;
    318 	sc->sc_rintr = NULL;
    319 
    320 	return 0;
    321 }
    322 
    323 static void
    324 vs_close(void *hdl)
    325 {
    326 	DPRINTF(1, ("vs_close\n"));
    327 }
    328 
    329 static int
    330 vs_query_encoding(void *hdl, struct audio_encoding *fp)
    331 {
    332 	DPRINTF(1, ("vs_query_encoding\n"));
    333 
    334 	if (fp->index >= sizeof(vs_encodings) / sizeof(vs_encodings[0]))
    335 		return EINVAL;
    336 
    337 	*fp = vs_encodings[fp->index];
    338 	return 0;
    339 }
    340 
    341 static int
    342 vs_round_sr(u_long rate)
    343 {
    344 	int i;
    345 	int diff = rate;
    346 	int nearest = 0;
    347 
    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 	struct audio_params *play, struct audio_params *rec)
    370 {
    371 	struct vs_softc *sc = hdl;
    372 	struct audio_params *p;
    373 	int mode;
    374 	int rate;
    375 
    376 	DPRINTF(1, ("vs_set_params: setmode=%d, usemode=%d\n", setmode, usemode));
    377 
    378 	/* set first record info, then play info */
    379 	for (mode = AUMODE_RECORD; mode != -1;
    380 	     mode = (mode == AUMODE_RECORD) ? AUMODE_PLAY : -1) {
    381 		if ((setmode & mode) == 0)
    382 			continue;
    383 
    384 		p = (mode == AUMODE_PLAY) ? play : rec;
    385 
    386 		if (p->channels != 1)
    387 			return (EINVAL);
    388 
    389 		rate = p->sample_rate;
    390 		p->sw_code = NULL;
    391 		p->factor = 1;
    392 		p->factor_denom = 1;
    393 		p->hw_precision = 4;
    394 		p->hw_encoding = AUDIO_ENCODING_ADPCM;
    395 		DPRINTF(1, ("vs_set_params: encoding=%d, precision=%d\n",
    396 			p->encoding, p->precision));
    397 		switch (p->encoding) {
    398 		case AUDIO_ENCODING_ULAW:
    399 			if (p->precision != 8)
    400 				return EINVAL;
    401 			if (mode == AUMODE_PLAY) {
    402 				p->sw_code = msm6258_mulaw_to_adpcm;
    403 				p->factor_denom = 2;
    404 			} else {
    405 				p->sw_code = msm6258_adpcm_to_mulaw;
    406 				p->factor = 2;
    407 			}
    408 			break;
    409 		case AUDIO_ENCODING_ULINEAR_LE:
    410 		case AUDIO_ENCODING_ULINEAR_BE:
    411 			if (p->precision != 8)
    412 				return EINVAL;
    413 			if (mode == AUMODE_PLAY) {
    414 				p->sw_code = msm6258_ulinear8_to_adpcm;
    415 				p->factor_denom = 2;
    416 			} else {
    417 				p->sw_code = msm6258_adpcm_to_ulinear8;
    418 				p->factor = 2;
    419 			}
    420 			break;
    421 		case AUDIO_ENCODING_ADPCM:
    422 			if (p->precision != 4)
    423 				return EINVAL;
    424 			break;
    425 		default:
    426 			DPRINTF(1, ("vs_set_params: mode=%d, encoding=%d\n",
    427 				mode, p->encoding));
    428 			return (EINVAL);
    429 		}
    430 		DPRINTF(1, ("vs_set_params: rate=%d -> ", rate));
    431 		rate = vs_round_sr(rate);
    432 		DPRINTF(1, ("%d\n", rate));
    433 		if (rate < 0)
    434 			return (EINVAL);
    435 		if (mode == AUMODE_PLAY)
    436 			sc->sc_current.prate = rate;
    437 		else
    438 			sc->sc_current.rrate = rate;
    439 	}
    440 
    441 	return 0;
    442 }
    443 
    444 static void
    445 vs_set_sr(struct vs_softc *sc, int rate)
    446 {
    447 	DPRINTF(1, ("setting sample rate to %d, %d\n",
    448 		 rate, (int)vs_l2r[rate].rate));
    449 	bus_space_write_1(sc->sc_iot, sc->sc_ppi, PPI_PORTC,
    450 			  (bus_space_read_1 (sc->sc_iot, sc->sc_ppi,
    451 					     PPI_PORTC) & 0xf0)
    452 			  | vs_l2r[rate].den);
    453 	adpcm_chgclk(vs_l2r[rate].clk);
    454 }
    455 
    456 static inline void
    457 vs_set_po(struct vs_softc *sc, u_long po)
    458 {
    459 	bus_space_write_1(sc->sc_iot, sc->sc_ppi, PPI_PORTC,
    460 			  (bus_space_read_1(sc->sc_iot, sc->sc_ppi, PPI_PORTC)
    461 			   & 0xfc) | po);
    462 }
    463 
    464 static int
    465 vs_trigger_output(void *hdl, void *start, void *end, int bsize,
    466 		  void (*intr)(void *), void *arg,
    467 		  struct audio_params *p)
    468 {
    469 	struct vs_softc *sc = hdl;
    470 	struct vs_dma *vd;
    471 	struct dmac_dma_xfer *xf;
    472 	struct dmac_channel_stat *chan = sc->sc_dma_ch;
    473 
    474 	DPRINTF(2, ("vs_trigger_output: start=%p, bsize=%d, intr=%p, arg=%p\n",
    475 		 start, bsize, intr, arg));
    476 
    477 	sc->sc_pintr = intr;
    478 	sc->sc_parg  = arg;
    479 	sc->sc_current.blksize = bsize;
    480 	sc->sc_current.bufsize = (char*)end - (char*)start;
    481 	sc->sc_current.dmap = 0;
    482 
    483 	/* Find DMA buffer. */
    484 	for (vd = sc->sc_dmas; vd != NULL && KVADDR(vd) != start;
    485 	     vd = vd->vd_next)
    486 		;
    487 	if (vd == NULL) {
    488 		printf("%s: trigger_output: bad addr %p\n",
    489 		    sc->sc_dev.dv_xname, start);
    490 		return (EINVAL);
    491 	}
    492 
    493 	vs_set_sr(sc, sc->sc_current.prate);
    494 	vs_set_po(sc, VS_PANOUT_LR);
    495 
    496 	xf = dmac_alloc_xfer (chan, sc->sc_dmat, vd->vd_map);
    497 	sc->sc_current.xfer = xf;
    498 	chan->ch_dcr = (DMAC_DCR_XRM_CSWOH | DMAC_DCR_OTYP_EASYNC |
    499 			DMAC_DCR_OPS_8BIT);
    500 	chan->ch_ocr = DMAC_OCR_REQG_EXTERNAL;
    501 	xf->dx_ocr = DMAC_OCR_DIR_MTD;
    502 	xf->dx_scr = DMAC_SCR_MAC_COUNT_UP | DMAC_SCR_DAC_NO_COUNT;
    503 	xf->dx_device = sc->sc_addr + MSM6258_DATA*2 + 1;
    504 
    505 	dmac_load_xfer (chan->ch_softc, xf);
    506 	dmac_start_xfer_offset (chan->ch_softc, xf, 0, sc->sc_current.blksize);
    507 	bus_space_write_1 (sc->sc_iot, sc->sc_ioh, MSM6258_STAT, 2);
    508 
    509 	return 0;
    510 }
    511 
    512 static int
    513 vs_trigger_input(void *hdl, void *start, void *end, int bsize,
    514 		 void (*intr)(void *), void *arg,
    515 		 struct audio_params *p)
    516 {
    517 	struct vs_softc *sc = hdl;
    518 	struct vs_dma *vd;
    519 	struct dmac_dma_xfer *xf;
    520 	struct dmac_channel_stat *chan = sc->sc_dma_ch;
    521 
    522 	DPRINTF(2, ("vs_trigger_input: start=%p, bsize=%d, intr=%p, arg=%p\n",
    523 		 start, bsize, intr, arg));
    524 
    525 	sc->sc_rintr = intr;
    526 	sc->sc_rarg  = arg;
    527 	sc->sc_current.blksize = bsize;
    528 	sc->sc_current.bufsize = (char*)end - (char*)start;
    529 	sc->sc_current.dmap = 0;
    530 
    531 	/* Find DMA buffer. */
    532 	for (vd = sc->sc_dmas; vd != NULL && KVADDR(vd) != start;
    533 	     vd = vd->vd_next)
    534 		;
    535 	if (vd == NULL) {
    536 		printf("%s: trigger_output: bad addr %p\n",
    537 		    sc->sc_dev.dv_xname, start);
    538 		return (EINVAL);
    539 	}
    540 
    541 	vs_set_sr(sc, sc->sc_current.rrate);
    542 	xf = dmac_alloc_xfer (chan, sc->sc_dmat, vd->vd_map);
    543 	sc->sc_current.xfer = xf;
    544 	chan->ch_dcr = (DMAC_DCR_XRM_CSWOH | DMAC_DCR_OTYP_EASYNC |
    545 			DMAC_DCR_OPS_8BIT);
    546 	chan->ch_ocr = DMAC_OCR_REQG_EXTERNAL;
    547 	xf->dx_ocr = DMAC_OCR_DIR_DTM;
    548 	xf->dx_scr = DMAC_SCR_MAC_COUNT_UP | DMAC_SCR_DAC_NO_COUNT;
    549 	xf->dx_device = sc->sc_addr + MSM6258_DATA*2 + 1;
    550 
    551 	dmac_load_xfer (chan->ch_softc, xf);
    552 	dmac_start_xfer_offset (chan->ch_softc, xf, 0, sc->sc_current.blksize);
    553 	bus_space_write_1 (sc->sc_iot, sc->sc_ioh, MSM6258_STAT, 4);
    554 
    555 	return 0;
    556 }
    557 
    558 static int
    559 vs_halt_output(void *hdl)
    560 {
    561 	struct vs_softc *sc = hdl;
    562 
    563 	DPRINTF(1, ("vs_halt_output\n"));
    564 
    565 	/* stop ADPCM play */
    566 	dmac_abort_xfer(sc->sc_dma_ch->ch_softc, sc->sc_current.xfer);
    567 	bus_space_write_1 (sc->sc_iot, sc->sc_ioh, MSM6258_STAT, 1);
    568 
    569 	return 0;
    570 }
    571 
    572 static int
    573 vs_halt_input(void *hdl)
    574 {
    575 	struct vs_softc *sc = hdl;
    576 
    577 	DPRINTF(1, ("vs_halt_input\n"));
    578 
    579 	/* stop ADPCM recoding */
    580 	dmac_abort_xfer(sc->sc_dma_ch->ch_softc, sc->sc_current.xfer);
    581 	bus_space_write_1 (sc->sc_iot, sc->sc_ioh, MSM6258_STAT, 1);
    582 
    583 	return 0;
    584 }
    585 
    586 static int
    587 vs_allocmem(sc, size, align, boundary, flags, vd)
    588 	struct vs_softc *sc;
    589 	size_t size;
    590 	size_t align;
    591 	size_t boundary;
    592 	int flags;
    593 	struct vs_dma *vd;
    594 {
    595 	int error, wait;
    596 
    597 #ifdef DIAGNOSTIC
    598 	if (size > DMAC_MAXSEGSZ)
    599 		panic ("vs_allocmem: maximum size exceeded, %d", (int) size);
    600 #endif
    601 
    602 	wait = (flags & M_NOWAIT) ? BUS_DMA_NOWAIT : BUS_DMA_WAITOK;
    603 	vd->vd_size = size;
    604 
    605 	error = bus_dmamem_alloc(vd->vd_dmat, vd->vd_size, align, boundary,
    606 				 vd->vd_segs,
    607 				 sizeof (vd->vd_segs) / sizeof (vd->vd_segs[0]),
    608 				 &vd->vd_nsegs, wait);
    609 	if (error)
    610 		goto out;
    611 
    612 	error = bus_dmamem_map(vd->vd_dmat, vd->vd_segs, vd->vd_nsegs,
    613 			       vd->vd_size, &vd->vd_addr,
    614 			       wait | BUS_DMA_COHERENT);
    615 	if (error)
    616 		goto free;
    617 
    618 	error = bus_dmamap_create(vd->vd_dmat, vd->vd_size, 1, DMAC_MAXSEGSZ,
    619 				  0, wait, &vd->vd_map);
    620 	if (error)
    621 		goto unmap;
    622 
    623 	error = bus_dmamap_load(vd->vd_dmat, vd->vd_map, vd->vd_addr,
    624 				vd->vd_size, NULL, wait);
    625 	if (error)
    626 		goto destroy;
    627 
    628 	return (0);
    629 
    630  destroy:
    631 	bus_dmamap_destroy(vd->vd_dmat, vd->vd_map);
    632  unmap:
    633 	bus_dmamem_unmap(vd->vd_dmat, vd->vd_addr, vd->vd_size);
    634  free:
    635 	bus_dmamem_free(vd->vd_dmat, vd->vd_segs, vd->vd_nsegs);
    636  out:
    637 	return (error);
    638 }
    639 
    640 static void
    641 vs_freemem(vd)
    642 	struct vs_dma *vd;
    643 {
    644 
    645 	bus_dmamap_unload(vd->vd_dmat, vd->vd_map);
    646 	bus_dmamap_destroy(vd->vd_dmat, vd->vd_map);
    647 	bus_dmamem_unmap(vd->vd_dmat, vd->vd_addr, vd->vd_size);
    648 	bus_dmamem_free(vd->vd_dmat, vd->vd_segs, vd->vd_nsegs);
    649 }
    650 
    651 static int
    652 vs_getdev(void *hdl, struct audio_device *retp)
    653 {
    654 	DPRINTF(1, ("vs_getdev\n"));
    655 
    656 	*retp = vs_device;
    657 	return 0;
    658 }
    659 
    660 static int
    661 vs_set_port(void *hdl, mixer_ctrl_t *cp)
    662 {
    663 	DPRINTF(1, ("vs_set_port\n"));
    664 	return 0;
    665 }
    666 
    667 static int
    668 vs_get_port(void *hdl, mixer_ctrl_t *cp)
    669 {
    670 	DPRINTF(1, ("vs_get_port\n"));
    671 	return 0;
    672 }
    673 
    674 static int
    675 vs_query_devinfo(void *hdl, mixer_devinfo_t *mi)
    676 {
    677 	DPRINTF(1, ("vs_query_devinfo\n"));
    678 	switch (mi->index) {
    679 	default:
    680 		return EINVAL;
    681 	}
    682 	return 0;
    683 }
    684 
    685 static void *
    686 vs_allocm(hdl, direction, size, type, flags)
    687 	void *hdl;
    688 	int direction;
    689 	size_t size;
    690 	int type, flags;
    691 {
    692 	struct vs_softc *sc = hdl;
    693 	struct vs_dma *vd;
    694 	int error;
    695 
    696 	if ((vd = malloc(size, type, flags)) == NULL)
    697 		return (NULL);
    698 
    699 	vd->vd_dmat = sc->sc_dmat;
    700 
    701 	error = vs_allocmem(sc, size, 32, 0, flags, vd);
    702 	if (error) {
    703 		free(vd, type);
    704 		return (NULL);
    705 	}
    706 	vd->vd_next = sc->sc_dmas;
    707 	sc->sc_dmas = vd;
    708 
    709 	return (KVADDR(vd));
    710 }
    711 
    712 static void
    713 vs_freem(hdl, addr, type)
    714 	void *hdl;
    715 	void *addr;
    716 	int type;
    717 {
    718 	struct vs_softc *sc = hdl;
    719 	struct vs_dma *p, **pp;
    720 
    721 	for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->vd_next) {
    722 		if (KVADDR(p) == addr) {
    723 			vs_freemem(p);
    724 			*pp = p->vd_next;
    725 			free(p, type);
    726 			return;
    727 		}
    728 	}
    729 }
    730 
    731 static size_t
    732 vs_round_buffersize(void *hdl, int direction, size_t bufsize)
    733 {
    734 	if (bufsize > DMAC_MAXSEGSZ)
    735 		bufsize = DMAC_MAXSEGSZ;
    736 
    737 	return bufsize;
    738 }
    739 
    740 #if 0
    741 paddr_t
    742 vs_mappage(addr, mem, off, prot)
    743 	void *addr;
    744 	void *mem;
    745 	off_t off;
    746 	int prot;
    747 {
    748 	struct vs_softc *sc = addr;
    749 	struct vs_dma *p;
    750 
    751 	if (off < 0)
    752 		return (-1);
    753 	for (p = sc->sc_dmas; p != NULL && KVADDR(p) != mem;
    754 	     p = p->vd_next)
    755 		;
    756 	if (p == NULL) {
    757 		printf("%s: mappage: bad addr %p\n",
    758 		    sc->sc_dev.dv_xname, start);
    759 		return (-1);
    760 	}
    761 
    762 	return (bus_dmamem_mmap(sc->sc_dmat, p->vd_segs, p->vd_nsegs,
    763 				off, prot, BUS_DMA_WAITOK));
    764 }
    765 #endif
    766 
    767 static int
    768 vs_get_props(void *hdl)
    769 {
    770 	DPRINTF(1, ("vs_get_props\n"));
    771 	return 0 /* | dependent | half duplex | no mmap */;
    772 }
    773 #endif /* NAUDIO > 0 && NVS > 0*/
    774