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