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