Home | History | Annotate | Line # | Download | only in iomd
vidcaudio.c revision 1.22
      1  1.22     bjh21 /*	$NetBSD: vidcaudio.c,v 1.22 2003/12/29 16:45:58 bjh21 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.1   reinoud /*
     33   1.1   reinoud  * audio driver for the RiscPC 16 bit sound
     34   1.1   reinoud  *
     35   1.1   reinoud  * Interfaces with the NetBSD generic audio driver to provide SUN
     36   1.1   reinoud  * /dev/audio (partial) compatibility.
     37   1.1   reinoud  */
     38   1.1   reinoud 
     39   1.1   reinoud #include <sys/param.h>	/* proc.h */
     40   1.5     bjh21 
     41  1.22     bjh21 __KERNEL_RCSID(0, "$NetBSD: vidcaudio.c,v 1.22 2003/12/29 16:45:58 bjh21 Exp $");
     42   1.5     bjh21 
     43  1.18     bjh21 #include <sys/audioio.h>
     44   1.1   reinoud #include <sys/conf.h>   /* autoconfig functions */
     45   1.1   reinoud #include <sys/device.h> /* device calls */
     46  1.18     bjh21 #include <sys/errno.h>
     47   1.1   reinoud #include <sys/proc.h>	/* device calls */
     48   1.1   reinoud #include <sys/systm.h>
     49   1.1   reinoud 
     50   1.1   reinoud #include <uvm/uvm_extern.h>
     51   1.1   reinoud 
     52   1.1   reinoud #include <dev/audio_if.h>
     53   1.1   reinoud 
     54   1.4   thorpej #include <machine/intr.h>
     55   1.3   thorpej #include <arm/arm32/katelib.h>
     56   1.1   reinoud 
     57   1.5     bjh21 #include <arm/iomd/vidcaudiovar.h>
     58   1.1   reinoud #include <arm/iomd/iomdreg.h>
     59   1.1   reinoud #include <arm/iomd/iomdvar.h>
     60   1.1   reinoud #include <arm/iomd/vidc.h>
     61   1.1   reinoud #include <arm/mainbus/mainbus.h>
     62   1.1   reinoud #include <arm/iomd/waveform.h>
     63   1.1   reinoud #include "vidcaudio.h"
     64   1.1   reinoud 
     65   1.1   reinoud extern int *vidc_base;
     66   1.1   reinoud 
     67   1.1   reinoud struct audio_general {
     68  1.18     bjh21 	vaddr_t	silence;
     69  1.18     bjh21 	irqhandler_t	ih;
     70   1.1   reinoud 
     71  1.18     bjh21 	void	(*intr)(void *);
     72  1.18     bjh21 	void	*arg;
     73   1.1   reinoud 
     74  1.18     bjh21 	paddr_t	next_cur;
     75  1.18     bjh21 	paddr_t	next_end;
     76  1.18     bjh21 	void	(*next_intr)(void *);
     77  1.18     bjh21 	void	*next_arg;
     78   1.1   reinoud 
     79  1.18     bjh21 	int	buffer;
     80  1.18     bjh21 	int	in_progress;
     81   1.1   reinoud 
     82  1.18     bjh21 	int	open;
     83   1.1   reinoud } ag;
     84   1.1   reinoud 
     85   1.1   reinoud struct vidcaudio_softc {
     86  1.18     bjh21 	struct	device device;
     87   1.1   reinoud 
     88  1.18     bjh21 	int	open;
     89   1.1   reinoud };
     90   1.1   reinoud 
     91  1.18     bjh21 int  vidcaudio_probe(struct device *, struct cfdata *, void *);
     92  1.18     bjh21 void vidcaudio_attach(struct device *, struct device *, void *);
     93  1.18     bjh21 int  vidcaudio_open(void *, int);
     94  1.18     bjh21 void vidcaudio_close(void *);
     95  1.18     bjh21 
     96  1.18     bjh21 int vidcaudio_intr(void *);
     97  1.18     bjh21 int vidcaudio_dma_program(vaddr_t, vaddr_t, void (*)(void *), void *);
     98  1.18     bjh21 void vidcaudio_dummy_routine(void *);
     99  1.18     bjh21 int vidcaudio_stereo(int, int);
    100  1.18     bjh21 int vidcaudio_rate(int);
    101  1.17     bjh21 void vidcaudio_shutdown(void);
    102   1.1   reinoud 
    103   1.1   reinoud static int sound_dma_intr;
    104   1.1   reinoud 
    105  1.12   thorpej CFATTACH_DECL(vidcaudio, sizeof(struct vidcaudio_softc),
    106  1.13   thorpej     vidcaudio_probe, vidcaudio_attach, NULL, NULL);
    107   1.1   reinoud 
    108  1.17     bjh21 int    vidcaudio_query_encoding(void *, struct audio_encoding *);
    109  1.18     bjh21 int    vidcaudio_set_params(void *, int, int, struct audio_params *,
    110  1.18     bjh21     struct audio_params *);
    111  1.17     bjh21 int    vidcaudio_round_blocksize(void *, int);
    112  1.17     bjh21 int    vidcaudio_start_output(void *, void *, int, void (*)(void *), void *);
    113  1.17     bjh21 int    vidcaudio_start_input(void *, void *, int, void (*)(void *), void *);
    114  1.17     bjh21 int    vidcaudio_halt_output(void *);
    115  1.17     bjh21 int    vidcaudio_halt_input(void *);
    116  1.17     bjh21 int    vidcaudio_speaker_ctl(void *, int);
    117  1.17     bjh21 int    vidcaudio_getdev(void *, struct audio_device *);
    118  1.17     bjh21 int    vidcaudio_set_port(void *, mixer_ctrl_t *);
    119  1.17     bjh21 int    vidcaudio_get_port(void *, mixer_ctrl_t *);
    120  1.17     bjh21 int    vidcaudio_query_devinfo(void *, mixer_devinfo_t *);
    121  1.17     bjh21 int    vidcaudio_get_props(void *);
    122   1.1   reinoud 
    123   1.1   reinoud struct audio_device vidcaudio_device = {
    124   1.1   reinoud 	"VidcAudio 8-bit",
    125   1.1   reinoud 	"x",
    126   1.1   reinoud 	"vidcaudio"
    127   1.1   reinoud };
    128   1.1   reinoud 
    129   1.1   reinoud struct audio_hw_if vidcaudio_hw_if = {
    130   1.1   reinoud 	vidcaudio_open,
    131   1.1   reinoud 	vidcaudio_close,
    132  1.19     bjh21 	NULL,
    133   1.1   reinoud 	vidcaudio_query_encoding,
    134   1.1   reinoud 	vidcaudio_set_params,
    135   1.1   reinoud 	vidcaudio_round_blocksize,
    136  1.19     bjh21 	NULL,
    137  1.19     bjh21 	NULL,
    138  1.19     bjh21 	NULL,
    139   1.1   reinoud 	vidcaudio_start_output,
    140   1.1   reinoud 	vidcaudio_start_input,
    141   1.1   reinoud 	vidcaudio_halt_output,
    142   1.1   reinoud 	vidcaudio_halt_input,
    143   1.1   reinoud 	vidcaudio_speaker_ctl,
    144   1.1   reinoud 	vidcaudio_getdev,
    145  1.19     bjh21 	NULL,
    146   1.1   reinoud 	vidcaudio_set_port,
    147   1.1   reinoud 	vidcaudio_get_port,
    148   1.1   reinoud 	vidcaudio_query_devinfo,
    149  1.19     bjh21 	NULL,
    150  1.19     bjh21 	NULL,
    151  1.19     bjh21 	NULL,
    152  1.19     bjh21 	NULL,
    153   1.1   reinoud 	vidcaudio_get_props,
    154  1.19     bjh21 	NULL,
    155  1.19     bjh21 	NULL,
    156  1.19     bjh21 	NULL,
    157   1.1   reinoud };
    158   1.1   reinoud 
    159   1.1   reinoud 
    160   1.1   reinoud void
    161  1.17     bjh21 vidcaudio_beep_generate(void)
    162   1.1   reinoud {
    163  1.18     bjh21 
    164   1.1   reinoud 	vidcaudio_dma_program(ag.silence, ag.silence+sizeof(beep_waveform)-16,
    165   1.1   reinoud 	    vidcaudio_dummy_routine, NULL);
    166   1.1   reinoud }
    167   1.1   reinoud 
    168   1.1   reinoud 
    169   1.1   reinoud int
    170  1.17     bjh21 vidcaudio_probe(struct device *parent, struct cfdata *cf, void *aux)
    171   1.1   reinoud {
    172   1.1   reinoud 	int id;
    173   1.1   reinoud 
    174   1.1   reinoud 	id = IOMD_ID;
    175   1.1   reinoud 
    176   1.1   reinoud 	/* So far I only know about this IOMD */
    177   1.1   reinoud 	switch (id) {
    178   1.1   reinoud 	case RPC600_IOMD_ID:
    179  1.18     bjh21 		return 1;
    180   1.1   reinoud 		break;
    181   1.1   reinoud 	case ARM7500_IOC_ID:
    182   1.1   reinoud 	case ARM7500FE_IOC_ID:
    183  1.18     bjh21 		return 1;
    184   1.1   reinoud 		break;
    185   1.1   reinoud 	default:
    186   1.1   reinoud 		printf("vidcaudio: Unknown IOMD id=%04x", id);
    187   1.1   reinoud 		break;
    188   1.1   reinoud 	}
    189   1.1   reinoud 
    190  1.18     bjh21 	return 0;
    191   1.1   reinoud }
    192   1.1   reinoud 
    193   1.1   reinoud 
    194   1.1   reinoud void
    195  1.17     bjh21 vidcaudio_attach(struct device *parent, struct device *self, void *aux)
    196   1.1   reinoud {
    197   1.1   reinoud 	struct vidcaudio_softc *sc = (void *)self;
    198   1.1   reinoud 	int id;
    199   1.1   reinoud 
    200   1.1   reinoud 	sc->open = 0;
    201   1.1   reinoud 	ag.in_progress = 0;
    202   1.1   reinoud 
    203   1.1   reinoud 	ag.next_cur = 0;
    204   1.1   reinoud 	ag.next_end = 0;
    205   1.1   reinoud 	ag.next_intr = NULL;
    206   1.1   reinoud 	ag.next_arg = NULL;
    207   1.1   reinoud 
    208  1.18     bjh21 	vidcaudio_rate(32); /* 24*1024 */
    209   1.1   reinoud 
    210   1.1   reinoud 	/* Program the silence buffer and reset the DMA channel */
    211  1.14   thorpej 	ag.silence = uvm_km_alloc(kernel_map, PAGE_SIZE);
    212  1.15  jdolecek 	if (ag.silence == 0)
    213  1.10    provos 		panic("vidcaudio: Cannot allocate memory");
    214   1.1   reinoud 
    215  1.14   thorpej 	memset((char *)ag.silence, 0, PAGE_SIZE);
    216   1.1   reinoud 	memcpy((char *)ag.silence, (char *)beep_waveform, sizeof(beep_waveform));
    217   1.1   reinoud 
    218   1.1   reinoud 	ag.buffer = 0;
    219   1.1   reinoud 
    220   1.1   reinoud 	/* Install the irq handler for the DMA interrupt */
    221   1.1   reinoud 	ag.ih.ih_func = vidcaudio_intr;
    222   1.1   reinoud 	ag.ih.ih_arg = NULL;
    223   1.1   reinoud 	ag.ih.ih_level = IPL_AUDIO;
    224   1.1   reinoud 	ag.ih.ih_name = "vidcaudio";
    225   1.1   reinoud 
    226   1.1   reinoud 	ag.intr = NULL;
    227   1.1   reinoud /*	ag.nextintr = NULL;*/
    228   1.1   reinoud 
    229   1.1   reinoud 	id = IOMD_ID;
    230   1.1   reinoud 
    231   1.1   reinoud 	switch (id) {
    232   1.1   reinoud 	case RPC600_IOMD_ID:
    233   1.1   reinoud 		sound_dma_intr = IRQ_DMASCH0;
    234   1.1   reinoud 		break;
    235   1.1   reinoud 	case ARM7500_IOC_ID:
    236   1.1   reinoud 	case ARM7500FE_IOC_ID:
    237   1.1   reinoud 		sound_dma_intr = IRQ_SDMA;
    238   1.1   reinoud 		break;
    239   1.1   reinoud 	}
    240   1.1   reinoud 
    241   1.1   reinoud 	disable_irq(sound_dma_intr);
    242   1.1   reinoud 
    243   1.1   reinoud 	if (irq_claim(sound_dma_intr, &(ag.ih)))
    244  1.10    provos 		panic("vidcaudio: couldn't claim IRQ %d", sound_dma_intr);
    245   1.1   reinoud 
    246   1.1   reinoud 	disable_irq(sound_dma_intr);
    247   1.1   reinoud 
    248   1.1   reinoud 	printf("\n");
    249   1.1   reinoud 
    250  1.21     bjh21 	vidcaudio_dma_program(ag.silence, ag.silence + PAGE_SIZE - 16,
    251   1.1   reinoud 	    vidcaudio_dummy_routine, NULL);
    252   1.1   reinoud 
    253   1.1   reinoud 	audio_attach_mi(&vidcaudio_hw_if, sc, &sc->device);
    254   1.1   reinoud 
    255  1.22     bjh21 #ifdef VIDCAUDIO_DEBUG
    256   1.1   reinoud 	printf(" UNDER DEVELOPMENT (nuts)\n");
    257   1.1   reinoud #endif
    258   1.1   reinoud }
    259   1.1   reinoud 
    260   1.1   reinoud int
    261  1.17     bjh21 vidcaudio_open(void *addr, int flags)
    262   1.1   reinoud {
    263   1.1   reinoud 	struct vidcaudio_softc *sc = addr;
    264   1.1   reinoud 
    265  1.22     bjh21 #ifdef VIDCAUDIO_DEBUG
    266   1.1   reinoud 	printf("DEBUG: vidcaudio_open called\n");
    267   1.1   reinoud #endif
    268   1.1   reinoud 
    269   1.1   reinoud 	if (sc->open)
    270   1.1   reinoud 		return EBUSY;
    271   1.1   reinoud 
    272   1.1   reinoud 	sc->open = 1;
    273   1.1   reinoud 	ag.open = 1;
    274   1.1   reinoud 
    275   1.1   reinoud 	return 0;
    276   1.1   reinoud }
    277   1.1   reinoud 
    278   1.1   reinoud void
    279  1.17     bjh21 vidcaudio_close(void *addr)
    280   1.1   reinoud {
    281   1.1   reinoud 	struct vidcaudio_softc *sc = addr;
    282   1.1   reinoud 
    283   1.1   reinoud 	vidcaudio_shutdown();
    284   1.1   reinoud 
    285  1.22     bjh21 #ifdef VIDCAUDIO_DEBUG
    286   1.1   reinoud 	printf("DEBUG: vidcaudio_close called\n");
    287   1.1   reinoud #endif
    288   1.1   reinoud 
    289   1.1   reinoud 	sc->open = 0;
    290   1.1   reinoud 	ag.open = 0;
    291   1.1   reinoud }
    292   1.1   reinoud 
    293  1.18     bjh21 /*
    294  1.18     bjh21  * Interface to the generic audio driver
    295  1.18     bjh21  */
    296   1.1   reinoud 
    297   1.1   reinoud int
    298  1.17     bjh21 vidcaudio_query_encoding(void *addr, struct audio_encoding *fp)
    299   1.1   reinoud {
    300  1.18     bjh21 
    301   1.1   reinoud 	switch (fp->index) {
    302   1.1   reinoud 	case 0:
    303   1.1   reinoud 		strcpy(fp->name, "vidc");
    304   1.1   reinoud 		fp->encoding = AUDIO_ENCODING_ULAW;
    305   1.1   reinoud 		fp->precision = 8;
    306   1.1   reinoud 		fp->flags = 0;
    307   1.1   reinoud 		break;
    308   1.1   reinoud 
    309   1.1   reinoud 	default:
    310  1.18     bjh21 		return EINVAL;
    311   1.1   reinoud 	}
    312   1.1   reinoud 	return 0;
    313   1.1   reinoud }
    314   1.1   reinoud 
    315   1.1   reinoud int
    316  1.17     bjh21 vidcaudio_set_params(void *addr, int setmode, int usemode,
    317  1.17     bjh21     struct audio_params *p, struct audio_params *r)
    318   1.1   reinoud {
    319  1.18     bjh21 
    320  1.16     bjh21 	if (p->encoding != AUDIO_ENCODING_ULAW)
    321   1.1   reinoud 		return EINVAL;
    322  1.16     bjh21 	/* FIXME Handle number of channels properly. */
    323   1.1   reinoud 	vidcaudio_rate(4 * p->sample_rate / (3 * 1024)); /* XXX probably wrong */
    324   1.1   reinoud 
    325   1.1   reinoud 	return 0;
    326   1.1   reinoud }
    327   1.1   reinoud 
    328   1.1   reinoud int
    329  1.17     bjh21 vidcaudio_round_blocksize(void *addr, int blk)
    330   1.1   reinoud {
    331  1.18     bjh21 
    332  1.14   thorpej 	if (blk > PAGE_SIZE)
    333  1.14   thorpej 		blk = PAGE_SIZE;
    334   1.1   reinoud 	return (blk);
    335   1.1   reinoud }
    336   1.1   reinoud 
    337  1.14   thorpej #define ROUND(s)  ( ((int)s) & (~(PAGE_SIZE-1)) )
    338   1.1   reinoud 
    339   1.1   reinoud int
    340  1.17     bjh21 vidcaudio_start_output(void *addr, void *p, int cc, void (*intr)(void *),
    341  1.17     bjh21     void *arg)
    342   1.1   reinoud {
    343  1.18     bjh21 
    344   1.1   reinoud 	/* I can only DMA inside 1 page */
    345   1.1   reinoud 
    346  1.22     bjh21 #ifdef VIDCAUDIO_DEBUG
    347  1.20     bjh21 	printf("vidcaudio_start_output (%d) %p %p\n", cc, intr, arg);
    348   1.1   reinoud #endif
    349   1.1   reinoud 
    350   1.1   reinoud 	if (ROUND(p) != ROUND(p+cc)) {
    351   1.1   reinoud 		/*
    352   1.1   reinoud 		 * If it's over a page I can fix it up by copying it into
    353   1.1   reinoud 		 * my buffer
    354   1.1   reinoud 		 */
    355   1.1   reinoud 
    356  1.22     bjh21 #ifdef VIDCAUDIO_DEBUG
    357   1.1   reinoud 		printf("vidcaudio: DMA over page boundary requested."
    358   1.1   reinoud 		    "  Fixing up\n");
    359   1.1   reinoud #endif
    360  1.14   thorpej 		memcpy(p, (char *)ag.silence, cc > PAGE_SIZE ? PAGE_SIZE : cc);
    361   1.1   reinoud 		p = (void *)ag.silence;
    362   1.1   reinoud 
    363   1.1   reinoud 		/*
    364   1.1   reinoud 		 * I can't DMA any more than that, but it is possible to
    365   1.1   reinoud 		 * fix it up by handling multiple buffers and only
    366   1.1   reinoud 		 * interrupting the audio driver after sending out all the
    367   1.1   reinoud 		 * stuff it gave me.  That it more than I can be bothered
    368   1.1   reinoud 		 * to do right now and it probablly wont happen so I'll just
    369   1.1   reinoud 		 * truncate the buffer and tell the user.
    370   1.1   reinoud 		 */
    371   1.1   reinoud 
    372  1.14   thorpej 		if (cc > PAGE_SIZE) {
    373   1.1   reinoud 			printf("vidcaudio: DMA buffer truncated. I could fix this up\n");
    374  1.14   thorpej 			cc = PAGE_SIZE;
    375   1.1   reinoud 		}
    376   1.1   reinoud 	}
    377   1.8   thorpej 	vidcaudio_dma_program((vaddr_t)p, (vaddr_t)((char *)p+cc),
    378   1.1   reinoud 	    intr, arg);
    379   1.1   reinoud 	return 0;
    380   1.1   reinoud }
    381   1.1   reinoud 
    382   1.1   reinoud int
    383  1.17     bjh21 vidcaudio_start_input(void *addr, void *p, int cc, void (*intr)(void *),
    384  1.17     bjh21     void *arg)
    385   1.1   reinoud {
    386   1.1   reinoud 	return EIO;
    387   1.1   reinoud }
    388   1.1   reinoud 
    389   1.1   reinoud int
    390  1.17     bjh21 vidcaudio_halt_output(void *addr)
    391   1.1   reinoud {
    392  1.18     bjh21 
    393  1.22     bjh21 #ifdef VIDCAUDIO_DEBUG
    394   1.1   reinoud 	printf("DEBUG: vidcaudio_halt_output\n");
    395   1.1   reinoud #endif
    396   1.1   reinoud 	return EIO;
    397   1.1   reinoud }
    398   1.1   reinoud 
    399   1.1   reinoud int
    400  1.17     bjh21 vidcaudio_halt_input(void *addr)
    401   1.1   reinoud {
    402  1.18     bjh21 
    403  1.22     bjh21 #ifdef VIDCAUDIO_DEBUG
    404   1.1   reinoud 	printf("DEBUG: vidcaudio_halt_input\n");
    405   1.1   reinoud #endif
    406   1.1   reinoud 	return EIO;
    407   1.1   reinoud }
    408   1.1   reinoud 
    409   1.1   reinoud int
    410  1.17     bjh21 vidcaudio_speaker_ctl(void *addr, int newstate)
    411   1.1   reinoud {
    412  1.18     bjh21 
    413  1.22     bjh21 #ifdef VIDCAUDIO_DEBUG
    414   1.1   reinoud 	printf("DEBUG: vidcaudio_speaker_ctl\n");
    415   1.1   reinoud #endif
    416   1.1   reinoud 	return 0;
    417   1.1   reinoud }
    418   1.1   reinoud 
    419   1.1   reinoud int
    420  1.17     bjh21 vidcaudio_getdev(void *addr, struct audio_device *retp)
    421   1.1   reinoud {
    422  1.18     bjh21 
    423   1.1   reinoud 	*retp = vidcaudio_device;
    424   1.1   reinoud 	return 0;
    425   1.1   reinoud }
    426   1.1   reinoud 
    427   1.1   reinoud 
    428   1.1   reinoud int
    429  1.17     bjh21 vidcaudio_set_port(void *addr, mixer_ctrl_t *cp)
    430   1.1   reinoud {
    431  1.18     bjh21 
    432   1.1   reinoud 	return EINVAL;
    433   1.1   reinoud }
    434   1.1   reinoud 
    435   1.1   reinoud int
    436  1.17     bjh21 vidcaudio_get_port(void *addr, mixer_ctrl_t *cp)
    437   1.1   reinoud {
    438  1.18     bjh21 
    439   1.1   reinoud 	return EINVAL;
    440   1.1   reinoud }
    441   1.1   reinoud 
    442   1.1   reinoud int
    443  1.17     bjh21 vidcaudio_query_devinfo(void *addr, mixer_devinfo_t *dip)
    444   1.1   reinoud {
    445  1.18     bjh21 
    446   1.1   reinoud 	return ENXIO;
    447   1.1   reinoud }
    448   1.1   reinoud 
    449   1.1   reinoud int
    450  1.17     bjh21 vidcaudio_get_props(void *addr)
    451   1.1   reinoud {
    452  1.18     bjh21 
    453   1.1   reinoud 	return 0;
    454   1.1   reinoud }
    455   1.1   reinoud void
    456  1.17     bjh21 vidcaudio_dummy_routine(void *arg)
    457   1.1   reinoud {
    458  1.18     bjh21 
    459  1.22     bjh21 #ifdef VIDCAUDIO_DEBUG
    460   1.1   reinoud 	printf("vidcaudio_dummy_routine\n");
    461   1.1   reinoud #endif
    462   1.1   reinoud }
    463   1.1   reinoud 
    464   1.1   reinoud int
    465  1.17     bjh21 vidcaudio_rate(int rate)
    466   1.1   reinoud {
    467  1.18     bjh21 
    468   1.1   reinoud 	WriteWord(vidc_base, VIDC_SFR | rate);
    469   1.1   reinoud 	return 0;
    470   1.1   reinoud }
    471   1.1   reinoud 
    472   1.1   reinoud int
    473  1.17     bjh21 vidcaudio_stereo(int channel, int position)
    474   1.1   reinoud {
    475  1.18     bjh21 
    476   1.1   reinoud 	if (channel < 0) return EINVAL;
    477   1.1   reinoud 	if (channel > 7) return EINVAL;
    478  1.18     bjh21 	channel = channel << 24 | VIDC_SIR0;
    479   1.1   reinoud 	WriteWord(vidc_base, channel | position);
    480   1.1   reinoud 	return 0;
    481   1.1   reinoud }
    482   1.1   reinoud 
    483   1.7   thorpej #define PHYS(x, y) pmap_extract(pmap_kernel(), ((x)&L2_S_FRAME), (paddr_t *)(y))
    484   1.1   reinoud 
    485   1.1   reinoud /*
    486   1.1   reinoud  * Program the next buffer to be used
    487   1.1   reinoud  * This function must be re-entrant, maximum re-entrancy of 2
    488   1.1   reinoud  */
    489   1.1   reinoud 
    490   1.1   reinoud #define FLAGS (0)
    491   1.1   reinoud 
    492   1.1   reinoud int
    493  1.17     bjh21 vidcaudio_dma_program(vaddr_t cur, vaddr_t end, void (*intr)(void *),
    494  1.17     bjh21     void *arg)
    495   1.1   reinoud {
    496   1.1   reinoud 	paddr_t pa1, pa2;
    497   1.1   reinoud 
    498   1.1   reinoud 	/* If there isn't a transfer in progress then start a new one */
    499   1.1   reinoud 	if (ag.in_progress == 0) {
    500   1.1   reinoud 		ag.buffer = 0;
    501   1.1   reinoud 		IOMD_WRITE_WORD(IOMD_SD0CR, 0x90);	/* Reset State Machine */
    502   1.1   reinoud 		IOMD_WRITE_WORD(IOMD_SD0CR, 0x30);	/* Reset State Machine */
    503   1.1   reinoud 
    504   1.1   reinoud 		PHYS(cur, &pa1);
    505   1.1   reinoud 		PHYS(end - 16, &pa2);
    506   1.1   reinoud 
    507   1.1   reinoud 		IOMD_WRITE_WORD(IOMD_SD0CURB, pa1);
    508   1.1   reinoud 		IOMD_WRITE_WORD(IOMD_SD0ENDB, pa2|FLAGS);
    509   1.1   reinoud 		IOMD_WRITE_WORD(IOMD_SD0CURA, pa1);
    510   1.1   reinoud 		IOMD_WRITE_WORD(IOMD_SD0ENDA, pa2|FLAGS);
    511   1.1   reinoud 
    512   1.1   reinoud 		ag.in_progress = 1;
    513   1.1   reinoud 
    514   1.1   reinoud 		ag.next_cur = ag.next_end = 0;
    515   1.1   reinoud 		ag.next_intr = ag.next_arg = 0;
    516   1.1   reinoud 
    517   1.1   reinoud 		ag.intr = intr;
    518   1.1   reinoud 		ag.arg = arg;
    519   1.1   reinoud 
    520   1.1   reinoud 		/*
    521   1.1   reinoud 		 * The driver 'clicks' between buffer swaps, leading me
    522   1.1   reinoud 		 * to think  that the fifo is much small than on other
    523   1.1   reinoud 		 * sound cards so I'm going to have to do some tricks here
    524   1.1   reinoud 		 */
    525   1.1   reinoud 
    526  1.18     bjh21 		(*ag.intr)(ag.arg);		/* Schedule the next buffer */
    527  1.18     bjh21 		ag.intr = vidcaudio_dummy_routine; /* Already done this     */
    528   1.1   reinoud 		ag.arg = NULL;
    529   1.1   reinoud 
    530   1.1   reinoud #ifdef PRINT
    531   1.1   reinoud 		printf("vidcaudio: start output\n");
    532   1.1   reinoud #endif
    533  1.22     bjh21 #ifdef VIDCAUDIO_DEBUG
    534   1.1   reinoud 		printf("SE");
    535   1.1   reinoud #endif
    536   1.1   reinoud 		enable_irq(sound_dma_intr);
    537   1.1   reinoud 	} else {
    538   1.1   reinoud 		/* Otherwise schedule the next one */
    539   1.1   reinoud 		if (ag.next_cur != 0) {
    540   1.1   reinoud 			/* If there's one scheduled then complain */
    541   1.1   reinoud 			printf("vidcaudio: Buffer already Q'ed\n");
    542   1.1   reinoud 			return EIO;
    543   1.1   reinoud 		} else {
    544   1.1   reinoud 			/* We're OK to schedule it now */
    545   1.1   reinoud 			ag.buffer = (++ag.buffer) & 1;
    546   1.1   reinoud 			PHYS(cur, &ag.next_cur);
    547   1.1   reinoud 			PHYS(end - 16, &ag.next_end);
    548   1.1   reinoud 			ag.next_intr = intr;
    549   1.1   reinoud 			ag.next_arg = arg;
    550  1.22     bjh21 #ifdef VIDCAUDIO_DEBUG
    551   1.1   reinoud 			printf("s");
    552   1.1   reinoud #endif
    553   1.1   reinoud 		}
    554   1.1   reinoud 	}
    555   1.1   reinoud 	return 0;
    556   1.1   reinoud }
    557   1.1   reinoud 
    558   1.1   reinoud void
    559   1.1   reinoud vidcaudio_shutdown(void)
    560   1.1   reinoud {
    561  1.18     bjh21 
    562   1.1   reinoud 	/* Shut down the channel */
    563   1.1   reinoud 	ag.intr = NULL;
    564   1.1   reinoud 	ag.in_progress = 0;
    565   1.1   reinoud #ifdef PRINT
    566   1.1   reinoud 	printf("vidcaudio: stop output\n");
    567   1.1   reinoud #endif
    568   1.1   reinoud 	IOMD_WRITE_WORD(IOMD_SD0CURB, ag.silence);
    569  1.18     bjh21 	IOMD_WRITE_WORD(IOMD_SD0ENDB,
    570  1.18     bjh21 	    (ag.silence + PAGE_SIZE - 16) | (1 << 30));
    571   1.1   reinoud 	disable_irq(sound_dma_intr);
    572   1.1   reinoud }
    573   1.1   reinoud 
    574   1.1   reinoud int
    575  1.17     bjh21 vidcaudio_intr(void *arg)
    576   1.1   reinoud {
    577   1.1   reinoud 	int status = IOMD_READ_BYTE(IOMD_SD0ST);
    578   1.5     bjh21 	void (*nintr)(void *);
    579   1.1   reinoud 	void *narg;
    580   1.5     bjh21 	void (*xintr)(void *);
    581   1.1   reinoud 	void *xarg;
    582   1.1   reinoud 	int xcur, xend;
    583  1.18     bjh21 
    584   1.1   reinoud 	IOMD_WRITE_WORD(IOMD_DMARQ, 0x10);
    585   1.1   reinoud 
    586   1.1   reinoud #ifdef PRINT
    587   1.1   reinoud 	printf ( "I" );
    588   1.1   reinoud #endif
    589   1.1   reinoud 
    590   1.1   reinoud 	if (ag.open == 0) {
    591   1.1   reinoud 		vidcaudio_shutdown();
    592   1.1   reinoud 		return 0;
    593   1.1   reinoud 	}
    594   1.1   reinoud 
    595   1.1   reinoud 	/* Have I got the generic audio device attached */
    596   1.1   reinoud 
    597  1.22     bjh21 #ifdef VIDCAUDIO_DEBUG
    598   1.1   reinoud 	printf ( "[B%01x]", status );
    599   1.1   reinoud #endif
    600   1.1   reinoud 
    601   1.1   reinoud 	nintr = ag.intr;
    602   1.1   reinoud 	narg = ag.arg;
    603   1.1   reinoud 	ag.intr = NULL;
    604   1.1   reinoud 
    605   1.1   reinoud 	xintr = ag.next_intr;
    606   1.1   reinoud 	xarg = ag.next_arg;
    607   1.1   reinoud 	xcur = ag.next_cur;
    608   1.1   reinoud 	xend = ag.next_end;
    609   1.1   reinoud 	ag.next_cur = 0;
    610   1.1   reinoud 	ag.intr = xintr;
    611   1.1   reinoud 	ag.arg = xarg;
    612   1.1   reinoud 
    613   1.1   reinoud 	if (nintr) {
    614  1.22     bjh21 #ifdef VIDCAUDIO_DEBUG
    615   1.1   reinoud 		printf("i");
    616   1.1   reinoud #endif
    617   1.1   reinoud 		(*nintr)(narg);
    618   1.1   reinoud 	}
    619   1.1   reinoud 
    620   1.1   reinoud 	if (xcur == 0) {
    621  1.18     bjh21 		vidcaudio_shutdown();
    622   1.1   reinoud 	} else {
    623   1.1   reinoud #define OVERRUN 	(0x04)
    624   1.1   reinoud #define INTERRUPT	(0x02)
    625   1.1   reinoud #define BANK_A		(0x00)
    626   1.1   reinoud #define BANK_B		(0x01)
    627   1.1   reinoud 		switch (status & 0x7) {
    628   1.1   reinoud 		case (INTERRUPT|BANK_A):
    629   1.1   reinoud #ifdef PRINT
    630   1.1   reinoud 			printf("B");
    631   1.1   reinoud #endif
    632   1.1   reinoud 			IOMD_WRITE_WORD(IOMD_SD0CURB, xcur);
    633   1.1   reinoud 			IOMD_WRITE_WORD(IOMD_SD0ENDB, xend|FLAGS);
    634   1.1   reinoud 			break;
    635   1.1   reinoud 
    636   1.1   reinoud 		case (INTERRUPT|BANK_B):
    637   1.1   reinoud #ifdef PRINT
    638   1.1   reinoud 			printf("A");
    639   1.1   reinoud #endif
    640   1.1   reinoud 			IOMD_WRITE_WORD(IOMD_SD0CURA, xcur);
    641   1.1   reinoud 			IOMD_WRITE_WORD(IOMD_SD0ENDA, xend|FLAGS);
    642   1.1   reinoud 			break;
    643   1.1   reinoud 
    644   1.1   reinoud 		case (OVERRUN|INTERRUPT|BANK_A):
    645   1.1   reinoud #ifdef PRINT
    646   1.1   reinoud 			printf("A");
    647   1.1   reinoud #endif
    648   1.1   reinoud 			IOMD_WRITE_WORD(IOMD_SD0CURA, xcur);
    649   1.1   reinoud 			IOMD_WRITE_WORD(IOMD_SD0ENDA, xend|FLAGS);
    650   1.1   reinoud 			break;
    651   1.1   reinoud 
    652   1.1   reinoud 		case (OVERRUN|INTERRUPT|BANK_B):
    653   1.1   reinoud #ifdef PRINT
    654   1.1   reinoud 			printf("B");
    655   1.1   reinoud #endif
    656   1.1   reinoud 			IOMD_WRITE_WORD(IOMD_SD0CURB, xcur);
    657   1.1   reinoud 			IOMD_WRITE_WORD(IOMD_SD0ENDB, xend|FLAGS);
    658   1.1   reinoud 			break;
    659   1.1   reinoud 		}
    660   1.1   reinoud /*
    661   1.1   reinoud 	ag.next_cur = 0;
    662   1.1   reinoud 	ag.intr = xintr;
    663   1.1   reinoud 	ag.arg = xarg;
    664   1.1   reinoud */
    665   1.1   reinoud 	}
    666   1.1   reinoud #ifdef PRINT
    667   1.1   reinoud 	printf ( "i" );
    668   1.1   reinoud #endif
    669   1.1   reinoud 
    670   1.1   reinoud 	if (ag.next_cur == 0) {
    671  1.18     bjh21 		(*ag.intr)(ag.arg);		/* Schedule the next buffer */
    672  1.18     bjh21 		ag.intr = vidcaudio_dummy_routine; /* Already done this     */
    673   1.1   reinoud 		ag.arg = NULL;
    674   1.1   reinoud 	}
    675  1.18     bjh21 	return 0 ;	/* Pass interrupt on down the chain */
    676   1.1   reinoud }
    677