Home | History | Annotate | Line # | Download | only in iomd
vidcaudio.c revision 1.50.2.2
      1  1.50.2.2  jdolecek /*	$NetBSD: vidcaudio.c,v 1.50.2.2 2017/12/03 11:35:54 jdolecek Exp $	*/
      2       1.1   reinoud 
      3       1.1   reinoud /*
      4       1.1   reinoud  * Copyright (c) 1995 Melvin Tang-Richardson
      5       1.1   reinoud  *
      6       1.1   reinoud  * Redistribution and use in source and binary forms, with or without
      7       1.1   reinoud  * modification, are permitted provided that the following conditions
      8       1.1   reinoud  * are met:
      9       1.1   reinoud  * 1. Redistributions of source code must retain the above copyright
     10       1.1   reinoud  *    notice, this list of conditions and the following disclaimer.
     11       1.1   reinoud  * 2. Redistributions in binary form must reproduce the above copyright
     12       1.1   reinoud  *    notice, this list of conditions and the following disclaimer in the
     13       1.1   reinoud  *    documentation and/or other materials provided with the distribution.
     14       1.1   reinoud  * 3. All advertising materials mentioning features or use of this software
     15       1.1   reinoud  *    must display the following acknowledgement:
     16       1.1   reinoud  *	This product includes software developed by the RiscBSD team.
     17       1.1   reinoud  * 4. The name of the author may not be used to endorse or promote products
     18       1.1   reinoud  *    derived from this software without specific prior written permission.
     19       1.1   reinoud  *
     20       1.1   reinoud  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21       1.1   reinoud  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22       1.1   reinoud  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23       1.1   reinoud  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24       1.1   reinoud  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25       1.1   reinoud  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26       1.1   reinoud  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27       1.1   reinoud  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28       1.1   reinoud  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29       1.1   reinoud  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30       1.1   reinoud  */
     31       1.1   reinoud 
     32      1.24     bjh21 /*-
     33      1.24     bjh21  * Copyright (c) 2003 Ben Harris
     34      1.24     bjh21  * All rights reserved.
     35      1.24     bjh21  *
     36      1.24     bjh21  * Redistribution and use in source and binary forms, with or without
     37      1.24     bjh21  * modification, are permitted provided that the following conditions
     38      1.24     bjh21  * are met:
     39      1.24     bjh21  * 1. Redistributions of source code must retain the above copyright
     40      1.24     bjh21  *    notice, this list of conditions and the following disclaimer.
     41      1.24     bjh21  * 2. Redistributions in binary form must reproduce the above copyright
     42      1.24     bjh21  *    notice, this list of conditions and the following disclaimer in the
     43      1.24     bjh21  *    documentation and/or other materials provided with the distribution.
     44      1.24     bjh21  * 3. The name of the author may not be used to endorse or promote products
     45      1.24     bjh21  *    derived from this software without specific prior written permission.
     46      1.24     bjh21  *
     47      1.24     bjh21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     48      1.24     bjh21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     49      1.24     bjh21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     50      1.24     bjh21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     51      1.24     bjh21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     52      1.24     bjh21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     53      1.24     bjh21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     54      1.24     bjh21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     55      1.24     bjh21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     56      1.24     bjh21  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     57      1.24     bjh21  */
     58      1.24     bjh21 
     59       1.1   reinoud /*
     60      1.24     bjh21  * audio driver for the RiscPC 8/16 bit sound
     61       1.1   reinoud  *
     62       1.1   reinoud  * Interfaces with the NetBSD generic audio driver to provide SUN
     63       1.1   reinoud  * /dev/audio (partial) compatibility.
     64       1.1   reinoud  */
     65       1.1   reinoud 
     66       1.1   reinoud #include <sys/param.h>	/* proc.h */
     67       1.5     bjh21 
     68  1.50.2.2  jdolecek __KERNEL_RCSID(0, "$NetBSD: vidcaudio.c,v 1.50.2.2 2017/12/03 11:35:54 jdolecek Exp $");
     69       1.5     bjh21 
     70      1.18     bjh21 #include <sys/audioio.h>
     71       1.1   reinoud #include <sys/conf.h>   /* autoconfig functions */
     72       1.1   reinoud #include <sys/device.h> /* device calls */
     73      1.18     bjh21 #include <sys/errno.h>
     74      1.30     bjh21 #include <sys/malloc.h>
     75       1.1   reinoud #include <sys/proc.h>	/* device calls */
     76       1.1   reinoud #include <sys/systm.h>
     77       1.1   reinoud 
     78       1.1   reinoud #include <uvm/uvm_extern.h>
     79       1.1   reinoud 
     80       1.1   reinoud #include <dev/audio_if.h>
     81      1.34     bjh21 #include <dev/audiobellvar.h>
     82      1.41      kent #include <dev/auconv.h>
     83      1.24     bjh21 #include <dev/mulaw.h>
     84       1.1   reinoud 
     85       1.4   thorpej #include <machine/intr.h>
     86      1.27     bjh21 #include <machine/machdep.h>
     87       1.1   reinoud 
     88       1.5     bjh21 #include <arm/iomd/vidcaudiovar.h>
     89       1.1   reinoud #include <arm/iomd/iomdreg.h>
     90       1.1   reinoud #include <arm/iomd/iomdvar.h>
     91       1.1   reinoud #include <arm/iomd/vidc.h>
     92       1.1   reinoud #include <arm/mainbus/mainbus.h>
     93       1.1   reinoud 
     94      1.39     bjh21 #include "pckbd.h"
     95      1.39     bjh21 #if NPCKBD > 0
     96      1.39     bjh21 #include <dev/pckbport/pckbdvar.h>
     97      1.39     bjh21 #endif
     98      1.39     bjh21 
     99       1.1   reinoud extern int *vidc_base;
    100       1.1   reinoud 
    101      1.28     bjh21 #ifdef VIDCAUDIO_DEBUG
    102      1.28     bjh21 #define DPRINTF(x)	printf x
    103      1.28     bjh21 #else
    104      1.28     bjh21 #define DPRINTF(x)
    105      1.28     bjh21 #endif
    106      1.28     bjh21 
    107  1.50.2.2  jdolecek #define WriteWord(a, b) \
    108  1.50.2.2  jdolecek *((volatile unsigned int *)(a)) = (b)
    109  1.50.2.2  jdolecek 
    110  1.50.2.2  jdolecek #define ReadWord(a) \
    111  1.50.2.2  jdolecek (*((volatile unsigned int *)(a)))
    112  1.50.2.2  jdolecek 
    113      1.24     bjh21 struct vidcaudio_softc {
    114      1.50     skrll 	device_t	sc_dev;
    115       1.1   reinoud 
    116      1.24     bjh21 	irqhandler_t	sc_ih;
    117      1.24     bjh21 	int	sc_dma_intr;
    118       1.1   reinoud 
    119      1.27     bjh21 	int	sc_is16bit;
    120       1.1   reinoud 
    121      1.24     bjh21 	size_t	sc_pblksize;
    122  1.50.2.2  jdolecek 	vaddr_t	sc_poffset;
    123  1.50.2.2  jdolecek 	vaddr_t	sc_pbufsize;
    124      1.30     bjh21 	paddr_t	*sc_ppages;
    125      1.24     bjh21 	void	(*sc_pintr)(void *);
    126      1.24     bjh21 	void	*sc_parg;
    127      1.24     bjh21 	int	sc_pcountdown;
    128      1.47  jmcneill 
    129      1.47  jmcneill 	kmutex_t	sc_lock;
    130      1.47  jmcneill 	kmutex_t	sc_intr_lock;
    131       1.1   reinoud };
    132       1.1   reinoud 
    133      1.50     skrll static int  vidcaudio_probe(device_t , cfdata_t , void *);
    134      1.50     skrll static void vidcaudio_attach(device_t , device_t , void *);
    135      1.25     bjh21 static void vidcaudio_close(void *);
    136      1.25     bjh21 
    137      1.25     bjh21 static int vidcaudio_intr(void *);
    138      1.25     bjh21 static void vidcaudio_rate(int);
    139      1.25     bjh21 static void vidcaudio_ctrl(int);
    140      1.25     bjh21 static void vidcaudio_stereo(int, int);
    141      1.41      kent static stream_filter_factory_t mulaw_to_vidc;
    142      1.41      kent static stream_filter_factory_t mulaw_to_vidc_stereo;
    143      1.47  jmcneill static int mulaw_to_vidc_fetch_to(struct audio_softc *, stream_fetcher_t *,
    144      1.47  jmcneill     audio_stream_t *, int);
    145      1.47  jmcneill static int mulaw_to_vidc_stereo_fetch_to(struct audio_softc *, stream_fetcher_t *,
    146      1.41      kent     audio_stream_t *, int);
    147       1.1   reinoud 
    148      1.50     skrll CFATTACH_DECL_NEW(vidcaudio, sizeof(struct vidcaudio_softc),
    149      1.13   thorpej     vidcaudio_probe, vidcaudio_attach, NULL, NULL);
    150       1.1   reinoud 
    151      1.25     bjh21 static int    vidcaudio_query_encoding(void *, struct audio_encoding *);
    152      1.41      kent static int    vidcaudio_set_params(void *, int, int, audio_params_t *,
    153      1.41      kent     audio_params_t *, stream_filter_list_t *, stream_filter_list_t *);
    154      1.41      kent static int    vidcaudio_round_blocksize(void *, int, int, const audio_params_t *);
    155      1.25     bjh21 static int    vidcaudio_trigger_output(void *, void *, void *, int,
    156      1.41      kent     void (*)(void *), void *, const audio_params_t *);
    157      1.25     bjh21 static int    vidcaudio_trigger_input(void *, void *, void *, int,
    158      1.41      kent     void (*)(void *), void *, const audio_params_t *);
    159      1.25     bjh21 static int    vidcaudio_halt_output(void *);
    160      1.25     bjh21 static int    vidcaudio_halt_input(void *);
    161      1.25     bjh21 static int    vidcaudio_getdev(void *, struct audio_device *);
    162      1.25     bjh21 static int    vidcaudio_set_port(void *, mixer_ctrl_t *);
    163      1.25     bjh21 static int    vidcaudio_get_port(void *, mixer_ctrl_t *);
    164      1.25     bjh21 static int    vidcaudio_query_devinfo(void *, mixer_devinfo_t *);
    165      1.25     bjh21 static int    vidcaudio_get_props(void *);
    166      1.47  jmcneill static void   vidcaudio_get_locks(void *, kmutex_t **, kmutex_t **);
    167       1.1   reinoud 
    168      1.25     bjh21 static struct audio_device vidcaudio_device = {
    169      1.24     bjh21 	"ARM VIDC",
    170      1.24     bjh21 	"",
    171       1.1   reinoud 	"vidcaudio"
    172       1.1   reinoud };
    173       1.1   reinoud 
    174      1.40      yamt static const struct audio_hw_if vidcaudio_hw_if = {
    175      1.41      kent 	NULL,			/* open */
    176       1.1   reinoud 	vidcaudio_close,
    177      1.19     bjh21 	NULL,
    178       1.1   reinoud 	vidcaudio_query_encoding,
    179       1.1   reinoud 	vidcaudio_set_params,
    180       1.1   reinoud 	vidcaudio_round_blocksize,
    181      1.19     bjh21 	NULL,
    182      1.19     bjh21 	NULL,
    183      1.19     bjh21 	NULL,
    184      1.24     bjh21 	NULL,
    185      1.24     bjh21 	NULL,
    186       1.1   reinoud 	vidcaudio_halt_output,
    187       1.1   reinoud 	vidcaudio_halt_input,
    188      1.23     bjh21 	NULL,
    189       1.1   reinoud 	vidcaudio_getdev,
    190      1.19     bjh21 	NULL,
    191       1.1   reinoud 	vidcaudio_set_port,
    192       1.1   reinoud 	vidcaudio_get_port,
    193       1.1   reinoud 	vidcaudio_query_devinfo,
    194      1.19     bjh21 	NULL,
    195      1.19     bjh21 	NULL,
    196      1.19     bjh21 	NULL,
    197      1.19     bjh21 	NULL,
    198       1.1   reinoud 	vidcaudio_get_props,
    199      1.24     bjh21 	vidcaudio_trigger_output,
    200      1.24     bjh21 	vidcaudio_trigger_input,
    201      1.19     bjh21 	NULL,
    202      1.47  jmcneill 	vidcaudio_get_locks,
    203       1.1   reinoud };
    204       1.1   reinoud 
    205      1.25     bjh21 static int
    206      1.50     skrll vidcaudio_probe(device_t parent, cfdata_t cf, void *aux)
    207       1.1   reinoud {
    208       1.1   reinoud 	int id;
    209       1.1   reinoud 
    210       1.1   reinoud 	id = IOMD_ID;
    211       1.1   reinoud 
    212       1.1   reinoud 	/* So far I only know about this IOMD */
    213       1.1   reinoud 	switch (id) {
    214       1.1   reinoud 	case RPC600_IOMD_ID:
    215       1.1   reinoud 	case ARM7500_IOC_ID:
    216       1.1   reinoud 	case ARM7500FE_IOC_ID:
    217      1.18     bjh21 		return 1;
    218       1.1   reinoud 	default:
    219      1.24     bjh21 		aprint_error("vidcaudio: Unknown IOMD id=%04x", id);
    220      1.24     bjh21 		return 0;
    221       1.1   reinoud 	}
    222       1.1   reinoud }
    223       1.1   reinoud 
    224       1.1   reinoud 
    225      1.25     bjh21 static void
    226      1.50     skrll vidcaudio_attach(device_t parent, device_t self, void *aux)
    227       1.1   reinoud {
    228      1.50     skrll 	struct vidcaudio_softc *sc = device_private(self);
    229      1.50     skrll 	device_t beepdev;
    230       1.1   reinoud 
    231      1.50     skrll 	sc->sc_dev = self;
    232      1.24     bjh21 	switch (IOMD_ID) {
    233      1.32     chris #ifndef EB7500ATX
    234       1.1   reinoud 	case RPC600_IOMD_ID:
    235      1.27     bjh21 		sc->sc_is16bit = (cmos_read(0xc4) >> 5) & 1;
    236      1.24     bjh21 		sc->sc_dma_intr = IRQ_DMASCH0;
    237       1.1   reinoud 		break;
    238      1.32     chris #endif
    239       1.1   reinoud 	case ARM7500_IOC_ID:
    240       1.1   reinoud 	case ARM7500FE_IOC_ID:
    241      1.45   thorpej 		sc->sc_is16bit = true;
    242      1.24     bjh21 		sc->sc_dma_intr = IRQ_SDMA;
    243       1.1   reinoud 		break;
    244      1.24     bjh21 	default:
    245      1.24     bjh21 		aprint_error(": strange IOMD\n");
    246      1.24     bjh21 		return;
    247       1.1   reinoud 	}
    248       1.1   reinoud 
    249      1.27     bjh21 	if (sc->sc_is16bit)
    250      1.27     bjh21 		aprint_normal(": 16-bit external DAC\n");
    251      1.27     bjh21 	else
    252      1.27     bjh21 		aprint_normal(": 8-bit internal DAC\n");
    253      1.27     bjh21 
    254      1.47  jmcneill 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    255      1.48       mrg 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_AUDIO);
    256      1.47  jmcneill 
    257      1.24     bjh21 	/* Install the irq handler for the DMA interrupt */
    258      1.24     bjh21 	sc->sc_ih.ih_func = vidcaudio_intr;
    259      1.24     bjh21 	sc->sc_ih.ih_arg = sc;
    260      1.48       mrg 	sc->sc_ih.ih_level = IPL_AUDIO;
    261  1.50.2.1       tls 	sc->sc_ih.ih_name = device_xname(self);
    262      1.24     bjh21 
    263      1.24     bjh21 	if (irq_claim(sc->sc_dma_intr, &sc->sc_ih) != 0) {
    264      1.24     bjh21 		aprint_error("%s: couldn't claim IRQ %d\n",
    265  1.50.2.1       tls 		    device_xname(self), sc->sc_dma_intr);
    266      1.24     bjh21 		return;
    267      1.24     bjh21 	}
    268       1.1   reinoud 
    269      1.24     bjh21 	disable_irq(sc->sc_dma_intr);
    270       1.1   reinoud 
    271      1.34     bjh21 	beepdev = audio_attach_mi(&vidcaudio_hw_if, sc, self);
    272      1.39     bjh21 #if NPCKBD > 0
    273      1.39     bjh21 	pckbd_hookup_bell(audiobell, beepdev);
    274      1.39     bjh21 #endif
    275       1.1   reinoud }
    276       1.1   reinoud 
    277      1.25     bjh21 static void
    278      1.17     bjh21 vidcaudio_close(void *addr)
    279       1.1   reinoud {
    280      1.42      kent 	struct vidcaudio_softc *sc;
    281       1.1   reinoud 
    282      1.28     bjh21 	DPRINTF(("DEBUG: vidcaudio_close called\n"));
    283      1.42      kent 	sc = addr;
    284      1.30     bjh21 	/*
    285      1.30     bjh21 	 * We do this here rather than in vidcaudio_halt_output()
    286      1.30     bjh21 	 * because the latter can be called from interrupt context
    287      1.30     bjh21 	 * (audio_pint()->audio_clear()->vidcaudio_halt_output()).
    288      1.30     bjh21 	 */
    289      1.30     bjh21 	if (sc->sc_ppages != NULL) {
    290      1.30     bjh21 		free(sc->sc_ppages, M_DEVBUF);
    291      1.30     bjh21 		sc->sc_ppages = NULL;
    292      1.30     bjh21 	}
    293       1.1   reinoud }
    294       1.1   reinoud 
    295      1.18     bjh21 /*
    296      1.18     bjh21  * Interface to the generic audio driver
    297      1.18     bjh21  */
    298       1.1   reinoud 
    299      1.25     bjh21 static int
    300      1.17     bjh21 vidcaudio_query_encoding(void *addr, struct audio_encoding *fp)
    301       1.1   reinoud {
    302      1.42      kent 	struct vidcaudio_softc *sc;
    303      1.18     bjh21 
    304      1.42      kent 	sc = addr;
    305       1.1   reinoud 	switch (fp->index) {
    306       1.1   reinoud 	case 0:
    307      1.24     bjh21 		strcpy(fp->name, AudioEmulaw);
    308       1.1   reinoud 		fp->encoding = AUDIO_ENCODING_ULAW;
    309       1.1   reinoud 		fp->precision = 8;
    310      1.27     bjh21 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    311       1.1   reinoud 		break;
    312       1.1   reinoud 
    313      1.24     bjh21 	case 1:
    314      1.27     bjh21 		if (sc->sc_is16bit) {
    315      1.24     bjh21 			strcpy(fp->name, AudioEslinear_le);
    316      1.24     bjh21 			fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
    317      1.24     bjh21 			fp->precision = 16;
    318      1.24     bjh21 			fp->flags = 0;
    319      1.24     bjh21 			break;
    320      1.24     bjh21 		}
    321      1.24     bjh21 		/* FALLTHROUGH */
    322       1.1   reinoud 	default:
    323      1.18     bjh21 		return EINVAL;
    324       1.1   reinoud 	}
    325       1.1   reinoud 	return 0;
    326       1.1   reinoud }
    327       1.1   reinoud 
    328      1.33     bjh21 #define MULAW_TO_VIDC(m) (~((m) << 1 | (m) >> 7))
    329      1.24     bjh21 
    330      1.41      kent static stream_filter_t *
    331      1.41      kent mulaw_to_vidc(struct audio_softc *sc, const audio_params_t *from,
    332      1.41      kent 	      const audio_params_t *to)
    333      1.41      kent {
    334      1.42      kent 
    335      1.41      kent 	return auconv_nocontext_filter_factory(mulaw_to_vidc_fetch_to);
    336      1.41      kent }
    337      1.41      kent 
    338      1.41      kent static int
    339      1.47  jmcneill mulaw_to_vidc_fetch_to(struct audio_softc *sc, stream_fetcher_t *self,
    340      1.47  jmcneill 		       audio_stream_t *dst, int max_used)
    341      1.24     bjh21 {
    342      1.41      kent 	stream_filter_t *this;
    343      1.41      kent 	int m, err;
    344      1.24     bjh21 
    345      1.41      kent 	this = (stream_filter_t *)self;
    346      1.47  jmcneill 	if ((err = this->prev->fetch_to(sc, this->prev, this->src, max_used)))
    347      1.41      kent 		return err;
    348      1.41      kent 	m = dst->end - dst->start;
    349      1.41      kent 	m = min(m, max_used);
    350      1.41      kent 	FILTER_LOOP_PROLOGUE(this->src, 1, dst, 1, m) {
    351      1.41      kent 		*d = MULAW_TO_VIDC(*s);
    352      1.41      kent 	} FILTER_LOOP_EPILOGUE(this->src, dst);
    353      1.41      kent 	return 0;
    354      1.41      kent }
    355      1.41      kent 
    356      1.41      kent static stream_filter_t *
    357      1.41      kent mulaw_to_vidc_stereo(struct audio_softc *sc, const audio_params_t *from,
    358      1.41      kent 		     const audio_params_t *to)
    359      1.41      kent {
    360      1.42      kent 
    361      1.41      kent 	return auconv_nocontext_filter_factory(mulaw_to_vidc_stereo_fetch_to);
    362      1.24     bjh21 }
    363      1.24     bjh21 
    364      1.41      kent static int
    365      1.47  jmcneill mulaw_to_vidc_stereo_fetch_to(struct audio_softc *sc, stream_fetcher_t *self,
    366      1.47  jmcneill 			      audio_stream_t *dst, int max_used)
    367      1.24     bjh21 {
    368      1.41      kent 	stream_filter_t *this;
    369      1.41      kent 	int m, err;
    370      1.24     bjh21 
    371      1.41      kent 	this = (stream_filter_t *)self;
    372      1.41      kent 	max_used = (max_used + 1) & ~1;
    373      1.47  jmcneill 	if ((err = this->prev->fetch_to(sc, this->prev, this->src, max_used / 2)))
    374      1.41      kent 		return err;
    375      1.41      kent 	m = (dst->end - dst->start) & ~1;
    376      1.41      kent 	m = min(m, max_used);
    377      1.41      kent 	FILTER_LOOP_PROLOGUE(this->src, 1, dst, 2, m) {
    378      1.41      kent 		d[0] = d[1] = MULAW_TO_VIDC(*s);
    379      1.41      kent 	} FILTER_LOOP_EPILOGUE(this->src, dst);
    380      1.41      kent 	return 0;
    381      1.24     bjh21 }
    382      1.24     bjh21 
    383      1.25     bjh21 static int
    384      1.17     bjh21 vidcaudio_set_params(void *addr, int setmode, int usemode,
    385      1.41      kent     audio_params_t *p, audio_params_t *r,
    386      1.41      kent     stream_filter_list_t *pfil, stream_filter_list_t *rfil)
    387       1.1   reinoud {
    388      1.41      kent 	audio_params_t hw;
    389      1.42      kent 	struct vidcaudio_softc *sc;
    390      1.24     bjh21 	int sample_period, ch;
    391      1.18     bjh21 
    392      1.36     bjh21 	if ((setmode & AUMODE_PLAY) == 0)
    393      1.37     bjh21 		return 0;
    394      1.36     bjh21 
    395      1.42      kent 	sc = addr;
    396      1.36     bjh21 	if (sc->sc_is16bit) {
    397      1.36     bjh21 		/* ARM7500ish, 16-bit, two-channel */
    398      1.41      kent 		hw = *p;
    399      1.36     bjh21 		if (p->encoding == AUDIO_ENCODING_ULAW && p->precision == 8) {
    400      1.41      kent 			hw.encoding = AUDIO_ENCODING_SLINEAR_LE;
    401      1.41      kent 			hw.precision = hw.validbits = 16;
    402      1.41      kent 			pfil->append(pfil, mulaw_to_linear16, &hw);
    403      1.36     bjh21 		} else if (p->encoding != AUDIO_ENCODING_SLINEAR_LE ||
    404      1.36     bjh21 		    p->precision != 16)
    405      1.36     bjh21 			return EINVAL;
    406      1.36     bjh21 		sample_period = 705600 / 4 / p->sample_rate;
    407      1.36     bjh21 		if (sample_period < 3) sample_period = 3;
    408      1.36     bjh21 		vidcaudio_rate(sample_period - 2);
    409      1.36     bjh21 		vidcaudio_ctrl(SCR_SERIAL);
    410      1.41      kent 		hw.sample_rate = 705600 / 4 / sample_period;
    411      1.41      kent 		hw.channels = 2;
    412      1.46     chris 		pfil->prepend(pfil, aurateconv, &hw);
    413      1.36     bjh21 	} else {
    414      1.36     bjh21 		/* VIDC20ish, u-law, 8-channel */
    415      1.36     bjh21 		if (p->encoding != AUDIO_ENCODING_ULAW || p->precision != 8)
    416      1.36     bjh21 			return EINVAL;
    417      1.36     bjh21 		/*
    418      1.36     bjh21 		 * We always use two hardware channels, because using
    419      1.36     bjh21 		 * one at 8kHz gives a nasty whining sound from the
    420      1.36     bjh21 		 * speaker.  The aurateconv mechanism doesn't support
    421      1.36     bjh21 		 * ulaw, so we do the channel duplication ourselves,
    422      1.36     bjh21 		 * and don't try to do rate conversion.
    423      1.36     bjh21 		 */
    424      1.36     bjh21 		sample_period = 1000000 / 2 / p->sample_rate;
    425      1.36     bjh21 		if (sample_period < 3) sample_period = 3;
    426      1.41      kent 		p->sample_rate = 1000000 / 2 / sample_period;
    427      1.41      kent 		hw = *p;
    428      1.41      kent 		hw.encoding = AUDIO_ENCODING_NONE;
    429      1.41      kent 		hw.precision = 8;
    430      1.36     bjh21 		vidcaudio_rate(sample_period - 2);
    431      1.36     bjh21 		vidcaudio_ctrl(SCR_SDAC | SCR_CLKSEL);
    432      1.41      kent 		if (p->channels == 1) {
    433      1.41      kent 			pfil->append(pfil, mulaw_to_vidc_stereo, &hw);
    434      1.36     bjh21 			for (ch = 0; ch < 8; ch++)
    435      1.36     bjh21 				vidcaudio_stereo(ch, SIR_CENTRE);
    436      1.41      kent 		} else {
    437      1.41      kent 			pfil->append(pfil, mulaw_to_vidc, &hw);
    438      1.36     bjh21 			for (ch = 0; ch < 8; ch += 2)
    439      1.36     bjh21 				vidcaudio_stereo(ch, SIR_LEFT_100);
    440      1.36     bjh21 			for (ch = 1; ch < 8; ch += 2)
    441      1.36     bjh21 				vidcaudio_stereo(ch, SIR_RIGHT_100);
    442      1.24     bjh21 		}
    443      1.24     bjh21 	}
    444       1.1   reinoud 	return 0;
    445       1.1   reinoud }
    446       1.1   reinoud 
    447      1.25     bjh21 static int
    448      1.41      kent vidcaudio_round_blocksize(void *addr, int wantblk,
    449      1.41      kent 			  int mode, const audio_params_t *param)
    450       1.1   reinoud {
    451      1.24     bjh21 	int blk;
    452      1.18     bjh21 
    453      1.42      kent 	/*
    454      1.24     bjh21 	 * Find the smallest power of two that's larger than the
    455      1.24     bjh21 	 * requested block size, but don't allow < 32 (DMA burst is 16
    456      1.24     bjh21 	 * bytes, and single bursts are tricky) or > PAGE_SIZE (DMA is
    457      1.24     bjh21 	 * confined to a page).
    458      1.24     bjh21 	 */
    459      1.24     bjh21 
    460      1.24     bjh21 	for (blk = 32; blk < PAGE_SIZE; blk <<= 1)
    461      1.42      kent 		if (blk >= wantblk)
    462      1.42      kent 			return blk;
    463      1.24     bjh21 	return blk;
    464       1.1   reinoud }
    465       1.1   reinoud 
    466      1.25     bjh21 static int
    467      1.24     bjh21 vidcaudio_trigger_output(void *addr, void *start, void *end, int blksize,
    468      1.41      kent     void (*intr)(void *), void *arg, const audio_params_t *params)
    469       1.1   reinoud {
    470      1.42      kent 	struct vidcaudio_softc *sc;
    471      1.30     bjh21 	size_t npages, i;
    472       1.1   reinoud 
    473      1.28     bjh21 	DPRINTF(("vidcaudio_trigger_output %p-%p/0x%x\n",
    474      1.28     bjh21 	    start, end, blksize));
    475       1.1   reinoud 
    476      1.42      kent 	sc = addr;
    477      1.41      kent 	KASSERT(blksize == vidcaudio_round_blocksize(addr, blksize, 0, NULL));
    478      1.30     bjh21 	KASSERT((vaddr_t)start % blksize == 0);
    479      1.30     bjh21 
    480      1.24     bjh21 	sc->sc_pblksize = blksize;
    481      1.30     bjh21 	sc->sc_pbufsize = (char *)end - (char *)start;
    482      1.30     bjh21 	npages = sc->sc_pbufsize >> PGSHIFT;
    483      1.30     bjh21 	if (sc->sc_ppages != NULL)
    484      1.30     bjh21 		free(sc->sc_ppages, M_DEVBUF);
    485      1.30     bjh21 	sc->sc_ppages = malloc(npages * sizeof(paddr_t), M_DEVBUF, M_WAITOK);
    486      1.49     skrll 	if (sc->sc_ppages == NULL)
    487      1.49     skrll 		return ENOMEM;
    488      1.30     bjh21 	for (i = 0; i < npages; i++)
    489      1.30     bjh21 		if (!pmap_extract(pmap_kernel(),
    490      1.30     bjh21 		    (vaddr_t)start + i * PAGE_SIZE, &sc->sc_ppages[i]))
    491      1.30     bjh21 			return EIO;
    492      1.30     bjh21 	sc->sc_poffset = 0;
    493      1.24     bjh21 	sc->sc_pintr = intr;
    494      1.24     bjh21 	sc->sc_parg = arg;
    495       1.1   reinoud 
    496      1.24     bjh21 	IOMD_WRITE_WORD(IOMD_SD0CR, IOMD_DMACR_CLEAR | IOMD_DMACR_QUADWORD);
    497      1.24     bjh21 	IOMD_WRITE_WORD(IOMD_SD0CR, IOMD_DMACR_ENABLE | IOMD_DMACR_QUADWORD);
    498      1.24     bjh21 
    499      1.24     bjh21 	/*
    500      1.24     bjh21 	 * Queue up the first two blocks, but don't tell audio code
    501      1.24     bjh21 	 * we're finished with them yet.
    502      1.24     bjh21 	 */
    503      1.24     bjh21 	sc->sc_pcountdown = 2;
    504      1.24     bjh21 
    505      1.24     bjh21 	enable_irq(sc->sc_dma_intr);
    506       1.1   reinoud 
    507       1.1   reinoud 	return 0;
    508       1.1   reinoud }
    509       1.1   reinoud 
    510      1.25     bjh21 static int
    511      1.24     bjh21 vidcaudio_trigger_input(void *addr, void *start, void *end, int blksize,
    512      1.41      kent     void (*intr)(void *), void *arg, const audio_params_t *params)
    513       1.1   reinoud {
    514      1.24     bjh21 
    515      1.24     bjh21 	return ENODEV;
    516       1.1   reinoud }
    517       1.1   reinoud 
    518      1.25     bjh21 static int
    519      1.17     bjh21 vidcaudio_halt_output(void *addr)
    520       1.1   reinoud {
    521      1.42      kent 	struct vidcaudio_softc *sc;
    522      1.18     bjh21 
    523      1.28     bjh21 	DPRINTF(("vidcaudio_halt_output\n"));
    524      1.42      kent 	sc = addr;
    525      1.30     bjh21 	disable_irq(sc->sc_dma_intr);
    526      1.24     bjh21 	IOMD_WRITE_WORD(IOMD_SD0CR, IOMD_DMACR_CLEAR | IOMD_DMACR_QUADWORD);
    527      1.24     bjh21 	return 0;
    528       1.1   reinoud }
    529       1.1   reinoud 
    530      1.25     bjh21 static int
    531      1.17     bjh21 vidcaudio_halt_input(void *addr)
    532       1.1   reinoud {
    533      1.18     bjh21 
    534      1.24     bjh21 	return ENODEV;
    535       1.1   reinoud }
    536       1.1   reinoud 
    537      1.25     bjh21 static int
    538      1.17     bjh21 vidcaudio_getdev(void *addr, struct audio_device *retp)
    539       1.1   reinoud {
    540      1.18     bjh21 
    541       1.1   reinoud 	*retp = vidcaudio_device;
    542       1.1   reinoud 	return 0;
    543       1.1   reinoud }
    544       1.1   reinoud 
    545       1.1   reinoud 
    546      1.25     bjh21 static int
    547      1.17     bjh21 vidcaudio_set_port(void *addr, mixer_ctrl_t *cp)
    548       1.1   reinoud {
    549      1.18     bjh21 
    550       1.1   reinoud 	return EINVAL;
    551       1.1   reinoud }
    552       1.1   reinoud 
    553      1.25     bjh21 static int
    554      1.17     bjh21 vidcaudio_get_port(void *addr, mixer_ctrl_t *cp)
    555       1.1   reinoud {
    556      1.18     bjh21 
    557       1.1   reinoud 	return EINVAL;
    558       1.1   reinoud }
    559       1.1   reinoud 
    560      1.25     bjh21 static int
    561      1.17     bjh21 vidcaudio_query_devinfo(void *addr, mixer_devinfo_t *dip)
    562       1.1   reinoud {
    563      1.18     bjh21 
    564       1.1   reinoud 	return ENXIO;
    565       1.1   reinoud }
    566       1.1   reinoud 
    567      1.25     bjh21 static int
    568      1.17     bjh21 vidcaudio_get_props(void *addr)
    569       1.1   reinoud {
    570      1.18     bjh21 
    571       1.1   reinoud 	return 0;
    572       1.1   reinoud }
    573      1.25     bjh21 
    574      1.25     bjh21 static void
    575      1.47  jmcneill vidcaudio_get_locks(void *opaque, kmutex_t **intr, kmutex_t **thread)
    576      1.47  jmcneill {
    577      1.47  jmcneill 	struct vidcaudio_softc *sc = opaque;
    578      1.47  jmcneill 
    579      1.47  jmcneill 	*intr = &sc->sc_intr_lock;
    580      1.47  jmcneill 	*thread = &sc->sc_lock;
    581      1.47  jmcneill }
    582      1.47  jmcneill 
    583      1.47  jmcneill static void
    584      1.24     bjh21 vidcaudio_rate(int rate)
    585       1.1   reinoud {
    586      1.18     bjh21 
    587      1.24     bjh21 	WriteWord(vidc_base, VIDC_SFR | rate);
    588       1.1   reinoud }
    589       1.1   reinoud 
    590      1.25     bjh21 static void
    591      1.24     bjh21 vidcaudio_ctrl(int ctrl)
    592       1.1   reinoud {
    593      1.18     bjh21 
    594      1.24     bjh21 	WriteWord(vidc_base, VIDC_SCR | ctrl);
    595       1.1   reinoud }
    596       1.1   reinoud 
    597      1.25     bjh21 static void
    598      1.17     bjh21 vidcaudio_stereo(int channel, int position)
    599       1.1   reinoud {
    600      1.18     bjh21 
    601      1.18     bjh21 	channel = channel << 24 | VIDC_SIR0;
    602       1.1   reinoud 	WriteWord(vidc_base, channel | position);
    603       1.1   reinoud }
    604       1.1   reinoud 
    605      1.25     bjh21 static int
    606      1.24     bjh21 vidcaudio_intr(void *arg)
    607       1.1   reinoud {
    608      1.42      kent 	struct vidcaudio_softc *sc;
    609      1.24     bjh21 	int status;
    610      1.24     bjh21 	paddr_t pnext, pend;
    611       1.1   reinoud 
    612      1.42      kent 	sc = arg;
    613      1.47  jmcneill 	mutex_spin_enter(&sc->sc_intr_lock);
    614      1.47  jmcneill 
    615      1.24     bjh21 	status = IOMD_READ_BYTE(IOMD_SD0ST);
    616      1.28     bjh21 	DPRINTF(("I[%x]", status));
    617      1.47  jmcneill 	if ((status & IOMD_DMAST_INT) == 0) {
    618      1.47  jmcneill 		mutex_spin_exit(&sc->sc_intr_lock);
    619      1.24     bjh21 		return 0;
    620      1.47  jmcneill 	}
    621       1.1   reinoud 
    622      1.30     bjh21 	pnext = sc->sc_ppages[sc->sc_poffset >> PGSHIFT] |
    623      1.30     bjh21 	    (sc->sc_poffset & PGOFSET);
    624      1.24     bjh21 	pend = (pnext + sc->sc_pblksize - 16) & IOMD_DMAEND_OFFSET;
    625       1.1   reinoud 
    626      1.24     bjh21 	switch (status &
    627      1.24     bjh21 	    (IOMD_DMAST_OVERRUN | IOMD_DMAST_INT | IOMD_DMAST_BANKB)) {
    628       1.1   reinoud 
    629      1.24     bjh21 	case (IOMD_DMAST_INT | IOMD_DMAST_BANKA):
    630      1.24     bjh21 	case (IOMD_DMAST_OVERRUN | IOMD_DMAST_INT | IOMD_DMAST_BANKB):
    631      1.28     bjh21 		DPRINTF(("B<0x%08lx,0x%03lx>", pnext, pend));
    632      1.24     bjh21 		IOMD_WRITE_WORD(IOMD_SD0CURB, pnext);
    633      1.24     bjh21 		IOMD_WRITE_WORD(IOMD_SD0ENDB, pend);
    634      1.24     bjh21 		break;
    635       1.1   reinoud 
    636      1.24     bjh21 	case (IOMD_DMAST_INT | IOMD_DMAST_BANKB):
    637      1.24     bjh21 	case (IOMD_DMAST_OVERRUN | IOMD_DMAST_INT | IOMD_DMAST_BANKA):
    638      1.28     bjh21 		DPRINTF(("A<0x%08lx,0x%03lx>", pnext, pend));
    639      1.24     bjh21 		IOMD_WRITE_WORD(IOMD_SD0CURA, pnext);
    640      1.24     bjh21 		IOMD_WRITE_WORD(IOMD_SD0ENDA, pend);
    641      1.24     bjh21 		break;
    642       1.1   reinoud 	}
    643       1.1   reinoud 
    644      1.30     bjh21 	sc->sc_poffset += sc->sc_pblksize;
    645      1.30     bjh21 	if (sc->sc_poffset >= sc->sc_pbufsize)
    646      1.30     bjh21 		sc->sc_poffset = 0;
    647      1.42      kent 
    648      1.24     bjh21 	if (sc->sc_pcountdown > 0)
    649      1.24     bjh21 		sc->sc_pcountdown--;
    650      1.24     bjh21 	else
    651      1.24     bjh21 		(*sc->sc_pintr)(sc->sc_parg);
    652       1.1   reinoud 
    653      1.47  jmcneill 	mutex_spin_exit(&sc->sc_intr_lock);
    654      1.24     bjh21 	return 1;
    655       1.1   reinoud }
    656