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