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