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