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