Home | History | Annotate | Line # | Download | only in dev
aucc.c revision 1.4
      1  1.4  is /*	$NetBSD: aucc.c,v 1.4 1997/06/20 21:45:11 is Exp $	*/
      2  1.1  is #undef AUDIO_DEBUG
      3  1.1  is /*
      4  1.1  is  * Copyright (c) 1997 Stephan Thesing
      5  1.1  is  * All rights reserved.
      6  1.1  is  *
      7  1.1  is  * Redistribution and use in source and binary forms, with or without
      8  1.1  is  * modification, are permitted provided that the following conditions
      9  1.1  is  * are met:
     10  1.1  is  * 1. Redistributions of source code must retain the above copyright
     11  1.1  is  *    notice, this list of conditions and the following disclaimer.
     12  1.1  is  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  is  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  is  *    documentation and/or other materials provided with the distribution.
     15  1.1  is  * 3. All advertising materials mentioning features or use of this software
     16  1.1  is  *    must display the following acknowledgement:
     17  1.1  is  *      This product includes software developed by Stephan Thesing.
     18  1.1  is  * 4. The name of the author may not be used to endorse or promote products
     19  1.1  is  *    derived from this software without specific prior written permission
     20  1.1  is  *
     21  1.1  is  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  1.1  is  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  1.1  is  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  1.1  is  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  1.1  is  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  1.1  is  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  1.1  is  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  1.1  is  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  1.1  is  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  1.1  is  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  1.1  is  */
     32  1.1  is 
     33  1.1  is #include "aucc.h"
     34  1.1  is #if NAUCC > 0
     35  1.1  is 
     36  1.1  is #include <sys/param.h>
     37  1.1  is #include <sys/systm.h>
     38  1.1  is #include <sys/errno.h>
     39  1.1  is #include <sys/ioctl.h>
     40  1.1  is #include <sys/device.h>
     41  1.1  is #include <sys/proc.h>
     42  1.1  is #include <machine/cpu.h>
     43  1.1  is 
     44  1.1  is #include <sys/audioio.h>
     45  1.1  is #include <dev/audio_if.h>
     46  1.1  is #include <amiga/amiga/cc.h>
     47  1.1  is #include <amiga/amiga/custom.h>
     48  1.1  is #include <amiga/amiga/device.h>
     49  1.1  is #include <amiga/dev/auccvar.h>
     50  1.1  is 
     51  1.1  is #ifdef AUDIO_DEBUG
     52  1.1  is /*extern printf __P((const char *,...));*/
     53  1.1  is int     auccdebug = 1;
     54  1.1  is #define DPRINTF(x)      if (auccdebug) printf x
     55  1.1  is #else
     56  1.1  is #define DPRINTF(x)
     57  1.1  is #endif
     58  1.1  is 
     59  1.1  is #ifdef splaudio
     60  1.1  is #undef splaudio
     61  1.1  is #endif
     62  1.1  is 
     63  1.1  is #define splaudio() spl4();
     64  1.1  is 
     65  1.1  is /* clock frequency.. */
     66  1.1  is extern int eclockfreq;
     67  1.1  is 
     68  1.1  is 
     69  1.1  is /* hw audio ch */
     70  1.1  is extern struct audio_channel channel[4];
     71  1.1  is 
     72  1.1  is 
     73  1.1  is /*
     74  1.1  is  * Software state.
     75  1.1  is  */
     76  1.1  is struct aucc_softc {
     77  1.1  is 	struct	device sc_dev;		/* base device */
     78  1.1  is 
     79  1.1  is 	int	sc_open;		/* single use device */
     80  1.1  is 	aucc_data_t sc_channel[4];	/* per channel freq, ... */
     81  1.1  is 	u_int	sc_encoding;		/* encoding AUDIO_ENCODING_.*/
     82  1.1  is 	int	sc_channels;		/* # of channels used */
     83  1.1  is 
     84  1.1  is 	int	sc_intrcnt;		/* interrupt count */
     85  1.1  is 	int	sc_channelmask;  	/* which channels are used ? */
     86  1.1  is };
     87  1.1  is 
     88  1.1  is /* interrupt interfaces */
     89  1.1  is void aucc_inthdl __P((int));
     90  1.1  is 
     91  1.1  is /* forward declarations */
     92  1.1  is static int init_aucc __P((struct aucc_softc *));
     93  1.1  is static u_int freqtoper  __P((u_int));
     94  1.1  is static u_int pertofreq  __P((u_int));
     95  1.1  is 
     96  1.1  is /* autoconfiguration driver */
     97  1.1  is void	auccattach __P((struct device *, struct device *, void *));
     98  1.1  is int	auccmatch __P((struct device *, struct cfdata *, void *));
     99  1.1  is 
    100  1.1  is struct cfattach aucc_ca = {
    101  1.1  is 	sizeof(struct aucc_softc),
    102  1.1  is 	auccmatch,
    103  1.1  is 	auccattach
    104  1.1  is };
    105  1.1  is 
    106  1.1  is struct	cfdriver aucc_cd = {
    107  1.1  is 	NULL, "aucc", DV_DULL, NULL, 0
    108  1.1  is };
    109  1.1  is 
    110  1.1  is struct audio_device aucc_device = {
    111  1.1  is 	"Amiga-audio",
    112  1.1  is 	"x",
    113  1.1  is 	"aucc"
    114  1.1  is };
    115  1.1  is 
    116  1.1  is 
    117  1.1  is struct aucc_softc *aucc=NULL;
    118  1.1  is 
    119  1.1  is 
    120  1.1  is unsigned char ulaw_to_lin[] = {
    121  1.2  is 	0x82, 0x86, 0x8a, 0x8e, 0x92, 0x96, 0x9a, 0x9e,
    122  1.2  is 	0xa2, 0xa6, 0xaa, 0xae, 0xb2, 0xb6, 0xba, 0xbe,
    123  1.2  is 	0xc1, 0xc3, 0xc5, 0xc7, 0xc9, 0xcb, 0xcd, 0xcf,
    124  1.2  is 	0xd1, 0xd3, 0xd5, 0xd7, 0xd9, 0xdb, 0xdd, 0xdf,
    125  1.2  is 	0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8,
    126  1.2  is 	0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0,
    127  1.2  is 	0xf0, 0xf1, 0xf1, 0xf2, 0xf2, 0xf3, 0xf3, 0xf4,
    128  1.2  is 	0xf4, 0xf5, 0xf5, 0xf6, 0xf6, 0xf7, 0xf7, 0xf8,
    129  1.2  is 	0xf8, 0xf8, 0xf9, 0xf9, 0xf9, 0xf9, 0xfa, 0xfa,
    130  1.2  is 	0xfa, 0xfa, 0xfb, 0xfb, 0xfb, 0xfb, 0xfc, 0xfc,
    131  1.2  is 	0xfc, 0xfc, 0xfc, 0xfc, 0xfd, 0xfd, 0xfd, 0xfd,
    132  1.2  is 	0xfd, 0xfd, 0xfd, 0xfd, 0xfe, 0xfe, 0xfe, 0xfe,
    133  1.2  is 	0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe,
    134  1.2  is 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    135  1.2  is 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    136  1.2  is 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
    137  1.2  is 	0x7d, 0x79, 0x75, 0x71, 0x6d, 0x69, 0x65, 0x61,
    138  1.2  is 	0x5d, 0x59, 0x55, 0x51, 0x4d, 0x49, 0x45, 0x41,
    139  1.2  is 	0x3e, 0x3c, 0x3a, 0x38, 0x36, 0x34, 0x32, 0x30,
    140  1.2  is 	0x2e, 0x2c, 0x2a, 0x28, 0x26, 0x24, 0x22, 0x20,
    141  1.2  is 	0x1e, 0x1d, 0x1c, 0x1b, 0x1a, 0x19, 0x18, 0x17,
    142  1.2  is 	0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x10, 0x0f,
    143  1.2  is 	0x0f, 0x0e, 0x0e, 0x0d, 0x0d, 0x0c, 0x0c, 0x0b,
    144  1.2  is 	0x0b, 0x0a, 0x0a, 0x09, 0x09, 0x08, 0x08, 0x07,
    145  1.2  is 	0x07, 0x07, 0x06, 0x06, 0x06, 0x06, 0x05, 0x05,
    146  1.2  is 	0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x03, 0x03,
    147  1.2  is 	0x03, 0x03, 0x03, 0x03, 0x02, 0x02, 0x02, 0x02,
    148  1.2  is 	0x02, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01, 0x01,
    149  1.2  is 	0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
    150  1.2  is 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    151  1.2  is 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    152  1.2  is 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    153  1.1  is };
    154  1.1  is 
    155  1.1  is /*
    156  1.1  is  * Define our interface to the higher level audio driver.
    157  1.1  is  */
    158  1.1  is int	aucc_open __P((dev_t, int));
    159  1.1  is void	aucc_close __P((void *));
    160  1.1  is int	aucc_set_out_sr __P((void *, u_long));
    161  1.1  is int	aucc_query_encoding __P((void *, struct audio_encoding *));
    162  1.1  is int	aucc_set_encoding __P((void *, u_int));
    163  1.1  is int	aucc_get_encoding __P((void *));
    164  1.1  is int	aucc_set_precision __P((void *, u_int));
    165  1.1  is int	aucc_get_precision __P((void *));
    166  1.1  is int	aucc_set_channels __P((void *, int));
    167  1.1  is int	aucc_get_channels __P((void *));
    168  1.1  is int	aucc_round_blocksize __P((void *, int));
    169  1.1  is int	aucc_set_out_port __P((void *, int));
    170  1.1  is int	aucc_get_out_port __P((void *));
    171  1.1  is int	aucc_set_in_port __P((void *, int));
    172  1.1  is int	aucc_get_in_port __P((void *));
    173  1.1  is int	aucc_commit_settings __P((void *));
    174  1.1  is int	aucc_start_output __P((void *, void *, int, void (*)(void *),
    175  1.1  is 				  void *));
    176  1.1  is int	aucc_start_input __P((void *, void *, int, void (*)(void *),
    177  1.1  is 				 void *));
    178  1.1  is int	aucc_halt_output __P((void *));
    179  1.1  is int	aucc_halt_input __P((void *));
    180  1.1  is int	aucc_cont_output __P((void *));
    181  1.1  is int	aucc_cont_input __P((void *));
    182  1.1  is int	aucc_getdev __P((void *, struct audio_device *));
    183  1.1  is int	aucc_setfd __P((void *, int));
    184  1.1  is int	aucc_set_port __P((void *, mixer_ctrl_t *));
    185  1.1  is int	aucc_get_port __P((void *, mixer_ctrl_t *));
    186  1.1  is int	aucc_query_devinfo __P((void *, mixer_devinfo_t *));
    187  1.1  is void	aucc_encode __P((int, u_char *, char *, int));
    188  1.1  is int	aucc_set_params __P((void *, int, struct audio_params *,
    189  1.1  is 	    struct audio_params *));
    190  1.1  is 
    191  1.1  is struct audio_hw_if sa_hw_if = {
    192  1.1  is 	aucc_open,
    193  1.1  is 	aucc_close,
    194  1.1  is 	NULL,
    195  1.1  is 	aucc_query_encoding,
    196  1.1  is 	aucc_set_params,
    197  1.1  is 	aucc_round_blocksize,
    198  1.1  is 	aucc_set_out_port,
    199  1.1  is 	aucc_get_out_port,
    200  1.1  is 	aucc_set_in_port,
    201  1.1  is 	aucc_get_in_port,
    202  1.1  is 	aucc_commit_settings,
    203  1.1  is 	aucc_start_output,
    204  1.1  is 	aucc_start_input,
    205  1.1  is 	aucc_halt_output,
    206  1.1  is 	aucc_halt_input,
    207  1.1  is 	aucc_cont_output,
    208  1.1  is 	aucc_cont_input,
    209  1.1  is 	NULL,
    210  1.1  is 	aucc_getdev,
    211  1.1  is 	aucc_setfd,
    212  1.1  is 	aucc_set_port,
    213  1.1  is 	aucc_get_port,
    214  1.1  is 	aucc_query_devinfo,
    215  1.1  is 	0,
    216  1.1  is 	0
    217  1.1  is };
    218  1.1  is 
    219  1.1  is /* autoconfig routines */
    220  1.1  is 
    221  1.1  is int
    222  1.1  is auccmatch(pdp, cfp, aux)
    223  1.1  is 	struct device *pdp;
    224  1.1  is 	struct cfdata *cfp;
    225  1.1  is 	void *aux;
    226  1.1  is {
    227  1.1  is 	if (matchname((char *)aux, "aucc") &&
    228  1.1  is #ifdef DRACO
    229  1.1  is 	    !is_draco() &&
    230  1.1  is #endif
    231  1.1  is 	    (cfp->cf_unit == 0))
    232  1.1  is 		return 1;
    233  1.1  is 
    234  1.1  is 	return 0;
    235  1.1  is }
    236  1.1  is 
    237  1.1  is /*
    238  1.1  is  * Audio chip found.
    239  1.1  is  */
    240  1.1  is void
    241  1.1  is auccattach(parent, self, args)
    242  1.1  is 	struct device *parent, *self;
    243  1.1  is 	void *args;
    244  1.1  is {
    245  1.1  is 	register struct aucc_softc *sc = (struct aucc_softc *)self;
    246  1.1  is 	register int i;
    247  1.1  is 
    248  1.1  is 	printf("\n");
    249  1.1  is 
    250  1.1  is 	if((i=init_aucc(sc))) {
    251  1.1  is 		printf("audio: no chipmem\n");
    252  1.1  is 		return;
    253  1.1  is 	}
    254  1.1  is 
    255  1.1  is 	if (audio_hardware_attach(&sa_hw_if, sc) != 0)
    256  1.1  is 		printf("audio: could not attach to audio pseudo-device driver\n");
    257  1.1  is 	/* XXX: no way to return error, if init fails */
    258  1.1  is }
    259  1.1  is 
    260  1.1  is 
    261  1.1  is static int
    262  1.1  is init_aucc(sc)
    263  1.1  is 	struct aucc_softc *sc;
    264  1.1  is {
    265  1.1  is 	register int i, err=0;
    266  1.1  is 
    267  1.1  is 	/* init values per channel */
    268  1.1  is  	for (i=0;i<4;i++) {
    269  1.1  is 		sc->sc_channel[i].nd_freq=8000;
    270  1.1  is 		sc->sc_channel[i].nd_per=freqtoper(8000);
    271  1.1  is 		sc->sc_channel[i].nd_busy=0;
    272  1.1  is 		sc->sc_channel[i].nd_dma=alloc_chipmem(AUDIO_BUF_SIZE*2);
    273  1.1  is 		if (sc->sc_channel[i].nd_dma==NULL)
    274  1.1  is 			err=1;
    275  1.1  is 	 	sc->sc_channel[i].nd_dmalength=0;
    276  1.1  is 		sc->sc_channel[i].nd_volume=64;
    277  1.1  is 		sc->sc_channel[i].nd_intr=NULL;
    278  1.1  is 		sc->sc_channel[i].nd_intrdata=NULL;
    279  1.1  is 		DPRINTF(("dma buffer for channel %d is %p\n", i,
    280  1.1  is 		    sc->sc_channel[i].nd_dma));
    281  1.1  is 
    282  1.1  is 	}
    283  1.1  is 
    284  1.1  is 	if (err) {
    285  1.1  is 		for(i=0;i<4;i++)
    286  1.1  is 			if (sc->sc_channel[i].nd_dma)
    287  1.1  is 				free_chipmem(sc->sc_channel[i].nd_dma);
    288  1.1  is 	}
    289  1.1  is 
    290  1.1  is 	sc->sc_channels=4;
    291  1.1  is 	sc->sc_channelmask=0xf;
    292  1.1  is 
    293  1.2  is 	/* clear interrupts and dma: */
    294  1.2  is 	custom.intena = INTF_AUD0|INTF_AUD1|INTF_AUD2|INTF_AUD3;
    295  1.2  is 	custom.dmacon = DMAF_AUD0|DMAF_AUD1|DMAF_AUD2|DMAF_AUD3;
    296  1.1  is 
    297  1.1  is 	sc->sc_encoding=AUDIO_ENCODING_ULAW;
    298  1.1  is 
    299  1.1  is 	return err;
    300  1.1  is 
    301  1.1  is }
    302  1.1  is 
    303  1.1  is int
    304  1.1  is aucc_open(dev, flags)
    305  1.1  is 	dev_t dev;
    306  1.1  is 	int flags;
    307  1.1  is {
    308  1.1  is 	register struct aucc_softc *sc;
    309  1.1  is 	int unit = AUDIOUNIT(dev);
    310  1.1  is 	register int i;
    311  1.1  is 
    312  1.1  is 	DPRINTF(("sa_open: unit %d\n",unit));
    313  1.1  is 
    314  1.1  is 	if (unit >= aucc_cd.cd_ndevs)
    315  1.1  is 		return (ENODEV);
    316  1.1  is 	if ((sc = aucc_cd.cd_devs[unit]) == NULL)
    317  1.1  is 		return (ENXIO);
    318  1.1  is 	if (sc->sc_open)
    319  1.1  is 		return (EBUSY);
    320  1.1  is 	sc->sc_open = 1;
    321  1.1  is 	for (i=0;i<4;i++) {
    322  1.1  is 		sc->sc_channel[i].nd_intr=NULL;
    323  1.1  is 		sc->sc_channel[i].nd_intrdata=NULL;
    324  1.1  is 	}
    325  1.1  is 	aucc=sc;
    326  1.1  is 
    327  1.1  is 	sc->sc_channels=4;
    328  1.1  is 	sc->sc_channelmask=0xf;
    329  1.1  is 
    330  1.1  is 	DPRINTF(("saopen: ok -> sc=0x%p\n",sc));
    331  1.1  is 
    332  1.1  is 	return (0);
    333  1.1  is }
    334  1.1  is 
    335  1.1  is void
    336  1.1  is aucc_close(addr)
    337  1.1  is 	void *addr;
    338  1.1  is {
    339  1.1  is 	register struct aucc_softc *sc = addr;
    340  1.1  is 
    341  1.1  is 	DPRINTF(("sa_close: sc=0x%p\n", sc));
    342  1.1  is 	/*
    343  1.1  is 	 * halt i/o, clear open flag, and done.
    344  1.1  is 	 */
    345  1.1  is 	aucc_halt_output(sc);
    346  1.1  is 	sc->sc_open = 0;
    347  1.1  is 
    348  1.1  is 	DPRINTF(("sa_close: closed.\n"));
    349  1.1  is }
    350  1.1  is 
    351  1.1  is int
    352  1.1  is aucc_set_out_sr(addr, sr)
    353  1.1  is 	void *addr;
    354  1.1  is 	u_long sr;
    355  1.1  is {
    356  1.1  is 	struct aucc_softc *sc=addr;
    357  1.1  is 	u_long per;
    358  1.1  is 	register int i;
    359  1.1  is 
    360  1.1  is 	per=freqtoper(sr);
    361  1.1  is 	if (per>0xffff)
    362  1.1  is 		return EINVAL;
    363  1.1  is 	sr=pertofreq(per);
    364  1.1  is 
    365  1.1  is 	for (i=0;i<4;i++) {
    366  1.1  is 		sc->sc_channel[i].nd_freq=sr;
    367  1.1  is 		sc->sc_channel[i].nd_per=per;
    368  1.1  is 	}
    369  1.1  is 
    370  1.1  is 	return(0);
    371  1.1  is }
    372  1.1  is 
    373  1.1  is int
    374  1.1  is aucc_query_encoding(addr, fp)
    375  1.1  is 	void *addr;
    376  1.1  is 	struct audio_encoding *fp;
    377  1.1  is {
    378  1.1  is 	switch (fp->index) {
    379  1.1  is 	case 0:
    380  1.1  is 		strcpy(fp->name, AudioElinear);
    381  1.1  is 		fp->encoding = AUDIO_ENCODING_LINEAR;
    382  1.1  is 		fp->precision = 8;
    383  1.1  is 		fp->flags = 0;
    384  1.1  is 		break;
    385  1.1  is 	case 1:
    386  1.1  is 		strcpy(fp->name, AudioEmulaw);
    387  1.1  is 		fp->encoding = AUDIO_ENCODING_ULAW;
    388  1.1  is 		fp->precision = 8;
    389  1.1  is 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    390  1.1  is 		break;
    391  1.1  is 
    392  1.3  is 	case 2:
    393  1.3  is 		strcpy(fp->name, AudioEulinear);
    394  1.3  is 		fp->encoding = AUDIO_ENCODING_ULINEAR;
    395  1.3  is 		fp->precision = 8;
    396  1.3  is 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    397  1.3  is 		break;
    398  1.3  is 
    399  1.1  is 	default:
    400  1.1  is 		return(EINVAL);
    401  1.1  is 		/*NOTREACHED*/
    402  1.1  is 	}
    403  1.1  is 	return(0);
    404  1.1  is }
    405  1.1  is 
    406  1.1  is int
    407  1.1  is aucc_set_params(addr, mode, p, q)
    408  1.1  is 	void *addr;
    409  1.1  is 	int mode;
    410  1.1  is 	struct  audio_params *p, *q;
    411  1.1  is {
    412  1.4  is 	if (mode == AUMODE_RECORD)
    413  1.4  is 		return 0 /*ENXIO*/;
    414  1.4  is 
    415  1.1  is 	switch (p->encoding) {
    416  1.1  is 	case AUDIO_ENCODING_ULAW:
    417  1.1  is 	case AUDIO_ENCODING_LINEAR:
    418  1.3  is 	case AUDIO_ENCODING_ULINEAR:
    419  1.1  is 		break;
    420  1.1  is 	default:
    421  1.1  is 		return EINVAL;
    422  1.1  is 		/* NOTREADCHED */
    423  1.1  is 	}
    424  1.1  is 
    425  1.1  is 	if (p->precision != 8)
    426  1.1  is 		return EINVAL;
    427  1.1  is 
    428  1.1  is 	if ((p->channels<1) || (p->channels>4))
    429  1.1  is 		return(EINVAL);
    430  1.1  is 
    431  1.1  is 	q->encoding = p->encoding;
    432  1.1  is 	q->precision = p->precision;
    433  1.1  is 	q->channels = p->channels;
    434  1.1  is 
    435  1.3  is 	aucc_set_out_sr(addr, p->sample_rate);
    436  1.1  is 	return 0;
    437  1.1  is }
    438  1.1  is 
    439  1.1  is int
    440  1.1  is aucc_get_encoding(addr)
    441  1.1  is 	void *addr;
    442  1.1  is {
    443  1.1  is 	return ((struct aucc_softc *)addr)->sc_encoding;
    444  1.1  is }
    445  1.1  is 
    446  1.1  is int
    447  1.1  is aucc_get_precision(addr)
    448  1.1  is 	void *addr;
    449  1.1  is {
    450  1.1  is 	return(8);
    451  1.1  is }
    452  1.1  is 
    453  1.1  is int
    454  1.1  is aucc_get_channels(addr)
    455  1.1  is 	void *addr;
    456  1.1  is {
    457  1.1  is 	return ((struct aucc_softc *)addr)->sc_channels;
    458  1.1  is }
    459  1.1  is 
    460  1.1  is int
    461  1.1  is aucc_round_blocksize(addr, blk)
    462  1.1  is 	void *addr;
    463  1.1  is 	int blk;
    464  1.1  is {
    465  1.1  is 
    466  1.1  is 
    467  1.1  is 	return blk>AUDIO_BUF_SIZE?AUDIO_BUF_SIZE:blk; /* round up to even size */
    468  1.1  is }
    469  1.1  is 
    470  1.1  is int
    471  1.1  is aucc_set_out_port(addr, port) /* can set channels  */
    472  1.1  is 	void *addr;
    473  1.1  is 	int port;
    474  1.1  is {
    475  1.1  is 	register struct aucc_softc *sc = addr;
    476  1.1  is 
    477  1.1  is 	/* port is mask for channels 0..3 */
    478  1.1  is 	if ((port<0)||(port>15))
    479  1.1  is 		return EINVAL;
    480  1.1  is 
    481  1.1  is 	sc->sc_channelmask=port;
    482  1.1  is 
    483  1.1  is 	return(0);
    484  1.1  is }
    485  1.1  is 
    486  1.1  is int
    487  1.1  is aucc_get_out_port(addr)
    488  1.1  is 	void *addr;
    489  1.1  is {
    490  1.1  is 	register struct aucc_softc *sc = addr;
    491  1.1  is 
    492  1.1  is 	return sc->sc_channelmask;
    493  1.1  is }
    494  1.1  is 
    495  1.1  is int
    496  1.1  is aucc_set_in_port(addr, port)
    497  1.1  is 	void *addr;
    498  1.1  is 	int port;
    499  1.1  is {
    500  1.1  is 	return(EINVAL); /* no input possible */
    501  1.1  is 
    502  1.1  is }
    503  1.1  is 
    504  1.1  is int
    505  1.1  is aucc_get_in_port(addr)
    506  1.1  is 	void *addr;
    507  1.1  is {
    508  1.1  is 	return(0);
    509  1.1  is }
    510  1.1  is 
    511  1.1  is int
    512  1.1  is aucc_commit_settings(addr)
    513  1.1  is 	void *addr;
    514  1.1  is {
    515  1.1  is 	register struct aucc_softc *sc = addr;
    516  1.1  is 	register int i;
    517  1.1  is 
    518  1.1  is 	DPRINTF(("sa_commit.\n"));
    519  1.1  is 
    520  1.1  is 	for (i=0;i<4;i++) {
    521  1.1  is 		custom.aud[i].vol=sc->sc_channel[i].nd_volume;
    522  1.1  is 		custom.aud[i].per=sc->sc_channel[i].nd_per;
    523  1.1  is 	}
    524  1.1  is 
    525  1.1  is 	DPRINTF(("commit done\n"));
    526  1.1  is 
    527  1.1  is 	return(0);
    528  1.1  is }
    529  1.1  is 
    530  1.1  is static int masks[4] = {1,3,7,15}; /* masks for n first channels */
    531  1.1  is static int masks2[4] = {1,2,4,8};
    532  1.1  is 
    533  1.1  is int
    534  1.1  is aucc_start_output(addr, p, cc, intr, arg)
    535  1.1  is 	void *addr;
    536  1.1  is 	void *p;
    537  1.1  is 	int cc;
    538  1.1  is 	void (*intr) __P((void *));
    539  1.1  is 	void *arg;
    540  1.1  is {
    541  1.1  is 	register struct aucc_softc *sc = addr;
    542  1.1  is 	register int mask=sc->sc_channelmask;
    543  1.1  is 	register int i,j=0;
    544  1.1  is 	register u_short *dmap;
    545  1.1  is 	register u_char *pp, *to;
    546  1.1  is 
    547  1.1  is 
    548  1.1  is 	dmap=NULL;
    549  1.1  is 
    550  1.1  is 	DPRINTF(("sa_start_output: cc=%d %p (%p)\n", cc, intr, arg));
    551  1.1  is 
    552  1.1  is 	mask &=masks[sc->sc_channels-1]; /* we use first sc_channels channels */
    553  1.1  is 	if (mask==0) /* active and used channels are disjoint */
    554  1.1  is 		return EINVAL;
    555  1.1  is 
    556  1.1  is 	for (i=0;i<4;i++) { /* channels available ? */
    557  1.1  is 		if ((masks2[i]&mask)&&(sc->sc_channel[i].nd_busy))
    558  1.1  is 			return EBUSY; /* channel is busy */
    559  1.1  is 		if (channel[i].isaudio==-1)
    560  1.1  is 			return EBUSY; /* system uses them */
    561  1.1  is 	}
    562  1.1  is 
    563  1.1  is 	/* enable interrupt on 1st channel */
    564  1.1  is 	for (i=0;i<4;i++) {
    565  1.1  is 		if (masks2[i]&mask) {
    566  1.1  is 			DPRINTF(("first channel is %d\n",i));
    567  1.1  is 			j=i;
    568  1.1  is 			sc->sc_channel[i].nd_intr=intr;
    569  1.1  is 			sc->sc_channel[i].nd_intrdata=arg;
    570  1.1  is 			dmap=sc->sc_channel[i].nd_dma;
    571  1.1  is 			break;
    572  1.1  is 		}
    573  1.1  is 	}
    574  1.1  is 
    575  1.1  is 	DPRINTF(("dmap is %p, mask=0x%x\n",dmap,mask));
    576  1.1  is 
    577  1.1  is 	/* copy data to dma buffer */
    578  1.1  is 
    579  1.1  is 
    580  1.1  is 	to=(u_char *)dmap;
    581  1.1  is 	pp=(u_char *)p;
    582  1.1  is 	aucc_encode(sc->sc_encoding, pp, to, cc);
    583  1.1  is 
    584  1.1  is 
    585  1.1  is 	/* disable ints, dma for channels, until all parameters set */
    586  1.1  is 	/* XXX custom.dmacon=mask;*/
    587  1.1  is 	custom.intreq=mask<<7;
    588  1.1  is 	custom.intena=mask<<7;
    589  1.1  is 
    590  1.1  is 
    591  1.1  is 	/* dma buffers: we use same buffer 4 all channels */
    592  1.1  is 	/* write dma location and length */
    593  1.1  is 	for (i=0;i<4;i++) {
    594  1.1  is 		if (masks2[i]&mask) {
    595  1.1  is 			DPRINTF(("turning channel %d on\n",i));
    596  1.1  is 			/*  sc->sc_channel[i].nd_busy=1;*/
    597  1.1  is 			channel[i].isaudio=1;
    598  1.1  is 			channel[i].play_count=1;
    599  1.1  is 			channel[i].handler=NULL;
    600  1.1  is 			custom.aud[i].per=sc->sc_channel[i].nd_per;
    601  1.1  is 			custom.aud[i].vol=sc->sc_channel[i].nd_volume;
    602  1.1  is 			if (custom.aud[i].lc==PREP_DMA_MEM(dmap))
    603  1.1  is 				custom.aud[i].lc =
    604  1.1  is 				    PREP_DMA_MEM(dmap+AUDIO_BUF_SIZE);
    605  1.1  is 			else
    606  1.1  is 				custom.aud[i].lc = PREP_DMA_MEM(dmap);
    607  1.1  is 
    608  1.1  is 			custom.aud[i].len=cc>>1;
    609  1.1  is 			sc->sc_channel[i].nd_mask=mask;
    610  1.1  is 			DPRINTF(("per is %d, vol is %d, len is %d\n",\
    611  1.1  is sc->sc_channel[i].nd_per, sc->sc_channel[i].nd_volume, cc>>1));
    612  1.1  is 
    613  1.1  is 		}
    614  1.1  is 	}
    615  1.1  is 
    616  1.1  is 	channel[j].handler=aucc_inthdl;
    617  1.1  is 
    618  1.1  is 	/* enable ints */
    619  1.1  is 	custom.intena=INTF_SETCLR|INTF_INTEN| (masks2[j]<<7);
    620  1.1  is 
    621  1.1  is 	DPRINTF(("enabled ints: 0x%x\n",(masks2[j]<<7)));
    622  1.1  is 
    623  1.1  is 	/* enable dma */
    624  1.1  is 	custom.dmacon=DMAF_SETCLR|DMAF_MASTER|mask;
    625  1.1  is 
    626  1.1  is 	DPRINTF(("enabled dma, mask=0x%x\n",mask));
    627  1.1  is 
    628  1.1  is 	return(0);
    629  1.1  is }
    630  1.1  is 
    631  1.1  is /* ARGSUSED */
    632  1.1  is int
    633  1.1  is aucc_start_input(addr, p, cc, intr, arg)
    634  1.1  is 	void *addr;
    635  1.1  is 	void *p;
    636  1.1  is 	int cc;
    637  1.1  is 	void (*intr) __P((void *));
    638  1.1  is 	void *arg;
    639  1.1  is {
    640  1.1  is 
    641  1.1  is 	return ENXIO; /* no input */
    642  1.1  is }
    643  1.1  is 
    644  1.1  is int
    645  1.1  is aucc_halt_output(addr)
    646  1.1  is 	void *addr;
    647  1.1  is {
    648  1.1  is 	register struct aucc_softc *sc = addr;
    649  1.1  is 	register int i;
    650  1.1  is 
    651  1.1  is 	/* XXX only halt, if input is also halted ?? */
    652  1.1  is 	/* stop dma, etc */
    653  1.1  is 	custom.intena=INTF_AUD0|INTF_AUD1|INTF_AUD2|INTF_AUD3;
    654  1.1  is 	custom.dmacon = DMAF_AUD0|DMAF_AUD1|DMAF_AUD2|DMAF_AUD3;
    655  1.1  is 	/* mark every busy unit idle */
    656  1.1  is 	for (i=0;i<4;i++) {
    657  1.1  is 		sc->sc_channel[i].nd_busy=sc->sc_channel[i].nd_mask=0;
    658  1.1  is 		channel[i].isaudio=0;
    659  1.1  is 		channel[i].play_count=0;
    660  1.1  is 	}
    661  1.1  is 
    662  1.1  is 	return(0);
    663  1.1  is }
    664  1.1  is 
    665  1.1  is int
    666  1.1  is aucc_halt_input(addr)
    667  1.1  is 	void *addr;
    668  1.1  is {
    669  1.1  is 	/* no input */
    670  1.1  is 
    671  1.1  is 	return ENXIO;
    672  1.1  is }
    673  1.1  is 
    674  1.1  is int
    675  1.1  is aucc_cont_output(addr)
    676  1.1  is 	void *addr;
    677  1.1  is {
    678  1.1  is 	DPRINTF(("aucc_cont_output: never called, what should it do?!\n"));
    679  1.1  is 	/* reenable DMA XXX */
    680  1.1  is 	return ENXIO;
    681  1.1  is }
    682  1.1  is 
    683  1.1  is int
    684  1.1  is aucc_cont_input(addr)
    685  1.1  is 	void *addr;
    686  1.1  is {
    687  1.1  is 	DPRINTF(("aucc_cont_input: never called, what should it do?!\n"));
    688  1.1  is 	return(0);
    689  1.1  is }
    690  1.1  is 
    691  1.1  is int
    692  1.1  is aucc_getdev(addr, retp)
    693  1.1  is         void *addr;
    694  1.1  is         struct audio_device *retp;
    695  1.1  is {
    696  1.1  is         *retp = aucc_device;
    697  1.1  is         return 0;
    698  1.1  is }
    699  1.1  is 
    700  1.1  is int
    701  1.1  is aucc_setfd(addr, flag)
    702  1.1  is         void *addr;
    703  1.1  is         int flag;
    704  1.1  is {
    705  1.1  is         return flag?EINVAL:0; /* Always half-duplex */
    706  1.1  is }
    707  1.1  is 
    708  1.1  is int
    709  1.1  is aucc_set_port(addr, cp)
    710  1.1  is 	void *addr;
    711  1.1  is 	mixer_ctrl_t *cp;
    712  1.1  is {
    713  1.1  is 	register struct aucc_softc *sc = addr;
    714  1.1  is 	register int i,j;
    715  1.1  is 
    716  1.1  is 	DPRINTF(("aucc_set_port: port=%d", cp->dev));
    717  1.1  is 
    718  1.1  is 	switch (cp->type) {
    719  1.1  is 	case AUDIO_MIXER_SET:
    720  1.1  is 		if (cp->dev!=AUCC_CHANNELS)
    721  1.1  is 			return EINVAL;
    722  1.1  is 		i=cp->un.mask;
    723  1.1  is 		if ((i<1)||(i>15))
    724  1.1  is 			return EINVAL;
    725  1.1  is 		sc->sc_channelmask=i;
    726  1.1  is 		break;
    727  1.1  is 
    728  1.1  is 	case AUDIO_MIXER_VALUE:
    729  1.1  is 		i=cp->un.value.num_channels;
    730  1.1  is 		if ((i<1)||(i>4))
    731  1.1  is 			return EINVAL;
    732  1.1  is 
    733  1.1  is 		if (cp->dev!=AUCC_VOLUME)
    734  1.1  is 			return EINVAL;
    735  1.1  is 
    736  1.1  is 		/* set volume for channel 0..i-1 */
    737  1.1  is 		for (j=0;j<i;j++)
    738  1.1  is 	 		sc->sc_channel[j].nd_volume=cp->un.value.level[j]>>2;
    739  1.1  is 			break;
    740  1.1  is 
    741  1.1  is 	default:
    742  1.1  is 		return EINVAL;
    743  1.1  is 		break;
    744  1.1  is 	}
    745  1.1  is 	return 0;
    746  1.1  is }
    747  1.1  is 
    748  1.1  is 
    749  1.1  is int
    750  1.1  is aucc_get_port(addr, cp)
    751  1.1  is 	void *addr;
    752  1.1  is 	mixer_ctrl_t *cp;
    753  1.1  is {
    754  1.1  is 	register struct aucc_softc *sc = addr;
    755  1.1  is 	register int i,j;
    756  1.1  is 
    757  1.1  is 	DPRINTF(("aucc_get_port: port=%d", cp->dev));
    758  1.1  is 
    759  1.1  is 	switch (cp->type) {
    760  1.1  is 	case AUDIO_MIXER_SET:
    761  1.1  is 		if (cp->dev!=AUCC_CHANNELS)
    762  1.1  is 			return EINVAL;
    763  1.1  is 		cp->un.mask=sc->sc_channelmask;
    764  1.1  is 		break;
    765  1.1  is 
    766  1.1  is 	case AUDIO_MIXER_VALUE:
    767  1.1  is 		i = cp->un.value.num_channels;
    768  1.1  is 		if ((i<1)||(i>4))
    769  1.1  is 			return EINVAL;
    770  1.1  is 
    771  1.1  is 		for (j=0;j<i;j++)
    772  1.1  is 			cp->un.value.level[j]=sc->sc_channel[j].nd_volume<<2;
    773  1.1  is 		break;
    774  1.1  is 
    775  1.1  is 	default:
    776  1.1  is 		return EINVAL;
    777  1.1  is 	}
    778  1.1  is 	return 0;
    779  1.1  is }
    780  1.1  is 
    781  1.1  is 
    782  1.1  is int
    783  1.1  is aucc_query_devinfo(addr, dip)
    784  1.1  is 	void *addr;
    785  1.1  is 	register mixer_devinfo_t *dip;
    786  1.1  is {
    787  1.1  is 	register int i;
    788  1.1  is 
    789  1.1  is 	switch(dip->index) {
    790  1.1  is 	case AUCC_CHANNELS:
    791  1.1  is 		dip->type = AUDIO_MIXER_SET;
    792  1.1  is 		dip->mixer_class = AUCC_OUTPUT_CLASS;
    793  1.1  is 		dip->prev = dip->next = AUDIO_MIXER_LAST;
    794  1.1  is                 strcpy(dip->label.name, AudioNspeaker);
    795  1.1  is 		for (i=0;i<16;i++) {
    796  1.1  is 			sprintf(dip->un.s.member[i].label.name,
    797  1.1  is 			    "channelmask%d", i);
    798  1.1  is 			dip->un.s.member[i].mask = i;
    799  1.1  is 		}
    800  1.1  is 		dip->un.s.num_mem = 16;
    801  1.1  is 		break;
    802  1.1  is 
    803  1.1  is 	case AUCC_VOLUME:
    804  1.1  is 		dip->type = AUDIO_MIXER_VALUE;
    805  1.1  is 		dip->mixer_class = AUCC_OUTPUT_CLASS;
    806  1.1  is 		dip->prev = dip->next = AUDIO_MIXER_LAST;
    807  1.1  is 		strcpy(dip->label.name, AudioNspeaker);
    808  1.1  is 		dip->un.v.num_channels = 4;
    809  1.1  is 		strcpy(dip->un.v.units.name, AudioNvolume);
    810  1.1  is 		break;
    811  1.1  is 
    812  1.1  is 	case AUCC_OUTPUT_CLASS:
    813  1.1  is 		dip->type = AUDIO_MIXER_CLASS;
    814  1.1  is 		dip->mixer_class = AUCC_OUTPUT_CLASS;
    815  1.1  is 		dip->next = dip->prev = AUDIO_MIXER_LAST;
    816  1.1  is 		strcpy(dip->label.name, AudioCOutputs);
    817  1.1  is 		break;
    818  1.1  is 	default:
    819  1.1  is 		return ENXIO;
    820  1.1  is 		/*NOTREACHED*/
    821  1.1  is 	}
    822  1.1  is 
    823  1.1  is 	DPRINTF(("AUDIO_MIXER_DEVINFO: name=%s\n", dip->label.name));
    824  1.1  is 
    825  1.1  is 	return(0);
    826  1.1  is }
    827  1.1  is 
    828  1.1  is 
    829  1.1  is /* audio int handler */
    830  1.1  is void
    831  1.1  is aucc_inthdl(int ch)
    832  1.1  is {
    833  1.1  is 	register int i;
    834  1.1  is 	register int mask=aucc->sc_channel[ch].nd_mask;
    835  1.1  is 
    836  1.1  is 	/* for all channels in this maskgroup:
    837  1.1  is 	   disable dma, int
    838  1.1  is 	   mark idle */
    839  1.1  is 	DPRINTF(("inthandler called, channel %d, mask 0x%x\n",ch,mask));
    840  1.1  is 
    841  1.1  is 	custom.intreq=mask<<7; /* clear request */
    842  1.1  is 	/* XXX: maybe we can leave ints and/or DMA on, if another sample has to be played?*/
    843  1.1  is 	custom.intena=mask<<7;
    844  1.1  is 	/*
    845  1.1  is 	 * XXX custom.dmacon=mask; NO!!!
    846  1.1  is 	 */
    847  1.1  is 	for (i=0;i<4;i++) {
    848  1.1  is 		if (masks2[i]&&mask) {
    849  1.1  is 			DPRINTF(("marking channel %d idle\n",i));
    850  1.1  is 			aucc->sc_channel[i].nd_busy=0;
    851  1.1  is 			aucc->sc_channel[i].nd_mask=0;
    852  1.1  is 			channel[i].isaudio=channel[i].play_count=0;
    853  1.1  is 		}
    854  1.1  is 	}
    855  1.1  is 
    856  1.1  is 	/* call handler */
    857  1.1  is 	if (aucc->sc_channel[ch].nd_intr) {
    858  1.1  is 		DPRINTF(("calling %p\n",aucc->sc_channel[ch].nd_intr));
    859  1.1  is 		(*(aucc->sc_channel[ch].nd_intr))(aucc->sc_channel[ch].nd_intrdata);
    860  1.1  is 	}
    861  1.1  is 	else DPRINTF(("zero int handler\n"));
    862  1.1  is 	DPRINTF(("ints done\n"));
    863  1.1  is }
    864  1.1  is 
    865  1.1  is 
    866  1.1  is 
    867  1.1  is 
    868  1.1  is /* transform frequency to period, adjust bounds */
    869  1.1  is static u_int
    870  1.1  is freqtoper(u_int freq)
    871  1.1  is {
    872  1.1  is 	u_int per=eclockfreq*5/freq;
    873  1.1  is 
    874  1.1  is 	if (per<124)
    875  1.1  is 		per=124; /* must have at least 124 ticks between samples */
    876  1.1  is 
    877  1.1  is 	return per;
    878  1.1  is }
    879  1.1  is 
    880  1.1  is /* transform period to frequency */
    881  1.1  is static u_int
    882  1.1  is pertofreq(u_int per)
    883  1.1  is {
    884  1.1  is 	u_int freq=eclockfreq*5/per;
    885  1.1  is 
    886  1.1  is 
    887  1.1  is 	return freq;
    888  1.1  is }
    889  1.1  is 
    890  1.1  is 
    891  1.1  is 
    892  1.1  is void
    893  1.1  is aucc_encode(enc, p, q, i)
    894  1.1  is 	int enc;
    895  1.1  is 	u_char *p;
    896  1.1  is 	char *q;
    897  1.1  is 	int i;
    898  1.1  is {
    899  1.1  is 	int off=0;
    900  1.1  is 	u_char *tab=NULL;
    901  1.1  is 
    902  1.1  is 
    903  1.1  is 
    904  1.1  is 	switch (enc) {
    905  1.1  is 	case AUDIO_ENCODING_ULAW:
    906  1.1  is 		tab=ulaw_to_lin;
    907  1.1  is 		break;
    908  1.1  is 	case AUDIO_ENCODING_ULINEAR:
    909  1.1  is 		off=-128;
    910  1.1  is 		/* FALLTHROUGH */
    911  1.1  is 	case AUDIO_ENCODING_LINEAR:
    912  1.1  is 		break;
    913  1.1  is 	default:
    914  1.1  is 		return;
    915  1.1  is 	}
    916  1.1  is 
    917  1.1  is 	if (tab)
    918  1.1  is 		while (i--)
    919  1.1  is 			*q++ = tab[*p++];
    920  1.1  is 	else
    921  1.1  is 		while (i--)
    922  1.1  is 			*q++ = *p++ + off;
    923  1.1  is 
    924  1.1  is }
    925  1.1  is 
    926  1.1  is #endif /* NAUCC > 0 */
    927