Home | History | Annotate | Line # | Download | only in isa
aria.c revision 1.9
      1 /*	$NetBSD: aria.c,v 1.9 2001/09/26 21:40:07 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1995, 1996, 1998 Roland C. Dowdeswell.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *      This product includes software developed by Roland C. Dowdeswell.
     17  * 4. The name of the authors may not be used to endorse or promote products
     18  *      derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*-
     33  * TODO:
     34  *  o   Test the driver on cards other than a single
     35  *      Prometheus Aria 16.
     36  *  o   Look into where aria_prometheus_kludge() belongs.
     37  *  o   Add some dma code.  It accomplishes its goal by
     38  *      direct IO at the moment.
     39  *  o   Different programs should be able to open the device
     40  *      with O_RDONLY and O_WRONLY at the same time.  But I
     41  *      do not see support for this in /sys/dev/audio.c, so
     42  *	I cannot effectively code it.
     43  *  o   We should nicely deal with the cards that can do mulaw
     44  *      and alaw output.
     45  *  o   Rework the mixer interface.
     46  *       o   Deal with the lvls better.  We need to do better mapping
     47  *           between logarithmic scales and the one byte that
     48  *           we are passed.
     49  *       o   Deal better with cards that have no mixer.
     50  */
     51 
     52 #include <sys/param.h>
     53 #include <sys/systm.h>
     54 #include <sys/errno.h>
     55 #include <sys/ioctl.h>
     56 #include <sys/syslog.h>
     57 #include <sys/device.h>
     58 #include <sys/proc.h>
     59 #include <sys/buf.h>
     60 
     61 #include <machine/cpu.h>
     62 #include <machine/bus.h>
     63 
     64 #include <sys/audioio.h>
     65 #include <dev/audio_if.h>
     66 #include <dev/auconv.h>
     67 
     68 #include <dev/mulaw.h>
     69 #include <dev/isa/isavar.h>
     70 #include <i386/isa/icu.h>
     71 
     72 #include <dev/isa/ariareg.h>
     73 
     74 #define FREAD 1
     75 #define FWRITE 2
     76 
     77 #ifdef AUDIO_DEBUG
     78 #define DPRINTF(x)	printf x
     79 int	ariadebug = 0;
     80 #else
     81 #define DPRINTF(x)
     82 #endif
     83 
     84 struct aria_mixdev_info {
     85 	u_char	num_channels;
     86 	u_char	level[2];
     87 	u_char	mute;
     88 };
     89 
     90 struct aria_mixmaster {
     91 	u_char num_channels;
     92 	u_char level[2];
     93 	u_char treble[2];
     94 	u_char bass[2];
     95 };
     96 
     97 struct aria_softc {
     98 	struct	device sc_dev;		/* base device */
     99 	void	*sc_ih;			/* interrupt vectoring */
    100 	bus_space_tag_t sc_iot;		/* Tag on 'da bus. */
    101 	bus_space_handle_t sc_ioh;	/* Handle of iospace */
    102 	isa_chipset_tag_t sc_ic;	/* ISA chipset info */
    103 
    104 	u_short	sc_open;		/* reference count of open calls */
    105 	u_short sc_play;		/* non-paused play chans 2**chan */
    106 	u_short sc_record;		/* non-paused record chans 2**chan */
    107 /* XXX -- keep this? */
    108 	u_short sc_gain[2];		/* left/right gain (play) */
    109 
    110 	u_long	sc_rate;		/* Sample rate for input and output */
    111 	u_int	sc_encoding;		/* audio encoding -- ulaw/linear */
    112 	int	sc_chans;		/* # of channels */
    113 	int	sc_precision;		/* # bits per sample */
    114 
    115 	u_long	sc_interrupts;		/* number of interrupts taken */
    116 	void	(*sc_rintr)(void*);	/* record transfer completion intr handler */
    117 	void	(*sc_pintr)(void*);	/* play transfer completion intr handler */
    118 	void	*sc_rarg;		/* arg for sc_rintr() */
    119 	void	*sc_parg;		/* arg for sc_pintr() */
    120 
    121 	int	sc_blocksize;		/* literal dio block size */
    122 	void	*sc_rdiobuffer;		/* record: where the next samples should be */
    123 	void	*sc_pdiobuffer;		/* play:   where the next samples are */
    124 
    125 	u_short sc_hardware;		/* bit field of hardware present */
    126 #define ARIA_TELEPHONE	0x0001		/* has telephone input */
    127 #define ARIA_MIXER	0x0002		/* has SC18075 digital mixer */
    128 #define ARIA_MODEL	0x0004		/* is SC18025 (=0) or SC18026 (=1) */
    129 
    130 	struct aria_mixdev_info aria_mix[6];
    131 	struct aria_mixmaster ariamix_master;
    132 	u_char	aria_mix_source;
    133 
    134 	int	sc_sendcmd_err;
    135 };
    136 
    137 int	ariaprobe __P((struct device *, struct cfdata *, void *));
    138 void	ariaattach __P((struct device *, struct device *, void *));
    139 void	ariaclose __P((void *));
    140 int	ariaopen __P((void *, int));
    141 int	ariareset __P((bus_space_tag_t, bus_space_handle_t));
    142 int	aria_reset __P((struct aria_softc *));
    143 int	aria_getdev __P((void *, struct audio_device *));
    144 
    145 void	aria_do_kludge __P((bus_space_tag_t, bus_space_handle_t,
    146 			    bus_space_handle_t,
    147 			    u_short, u_short, u_short, u_short));
    148 void	aria_prometheus_kludge __P((struct isa_attach_args *,
    149 				    bus_space_handle_t));
    150 
    151 int	aria_query_encoding __P((void *, struct audio_encoding *));
    152 int	aria_round_blocksize __P((void *, int));
    153 int	aria_speaker_ctl __P((void *, int));
    154 int	aria_commit_settings __P((void *));
    155 int	aria_set_params __P((void *, int, int,
    156 			     struct audio_params *, struct audio_params *));
    157 int	aria_get_props __P((void *));
    158 
    159 int	aria_start_output __P((void *, void *, int,
    160 			       void (*) __P((void *)), void*));
    161 int	aria_start_input __P((void *, void *, int,
    162 			      void (*) __P((void *)), void*));
    163 
    164 int	aria_halt_input __P((void *));
    165 int	aria_halt_output __P((void *));
    166 
    167 int	aria_sendcmd __P((struct aria_softc *, u_short, int, int, int));
    168 
    169 u_short	aria_getdspmem __P((struct aria_softc *, u_short));
    170 void	aria_putdspmem __P((struct aria_softc *, u_short, u_short));
    171 
    172 int	aria_intr __P((void *));
    173 short	ariaversion __P((struct aria_softc *));
    174 
    175 void	aria_set_mixer __P((struct aria_softc *, int));
    176 
    177 void	aria_mix_write __P((struct aria_softc *, int, int));
    178 int	aria_mix_read __P((struct aria_softc *, int));
    179 
    180 int	aria_mixer_set_port __P((void *, mixer_ctrl_t *));
    181 int	aria_mixer_get_port __P((void *, mixer_ctrl_t *));
    182 int	aria_mixer_query_devinfo __P((void *, mixer_devinfo_t *));
    183 
    184 struct cfattach aria_ca = {
    185 	sizeof(struct aria_softc), ariaprobe, ariaattach
    186 };
    187 
    188 /* XXX temporary test for 1.3 */
    189 #ifndef AudioNaux
    190 /* 1.3 */
    191 struct cfdriver aria_cd = {
    192 	NULL, "aria", DV_DULL
    193 };
    194 #endif
    195 
    196 struct audio_device aria_device = {
    197 	"Aria 16(se)",
    198 	"x",
    199 	"aria"
    200 };
    201 
    202 /*
    203  * Define our interface to the higher level audio driver.
    204  */
    205 
    206 struct audio_hw_if aria_hw_if = {
    207 	ariaopen,
    208 	ariaclose,
    209 	NULL,
    210 	aria_query_encoding,
    211 	aria_set_params,
    212 	aria_round_blocksize,
    213 	aria_commit_settings,
    214 	NULL,
    215 	NULL,
    216 	aria_start_output,
    217 	aria_start_input,
    218 	aria_halt_input,
    219 	aria_halt_output,
    220 	NULL,
    221 	aria_getdev,
    222 	NULL,
    223 	aria_mixer_set_port,
    224 	aria_mixer_get_port,
    225 	aria_mixer_query_devinfo,
    226 	NULL,
    227 	NULL,
    228 	NULL,
    229 	NULL,
    230 	aria_get_props,
    231 };
    232 
    233 /*
    234  * Probe / attach routines.
    235  */
    236 
    237 /*
    238  * Probe for the aria hardware.
    239  */
    240 int
    241 ariaprobe(parent, cf, aux)
    242 	struct device *parent;
    243 	struct cfdata *cf;
    244 	void *aux;
    245 {
    246 	bus_space_handle_t ioh;
    247 	struct isa_attach_args *ia = aux;
    248 
    249 	if (!ARIA_BASE_VALID(ia->ia_iobase)) {
    250 		printf("aria: configured iobase %d invalid\n", ia->ia_iobase);
    251 		return 0;
    252 	}
    253 
    254 	if (!ARIA_IRQ_VALID(ia->ia_irq)) {
    255 		printf("aria: configured irq %d invalid\n", ia->ia_irq);
    256 		return 0;
    257 	}
    258 
    259 	if (bus_space_map(ia->ia_iot, ia->ia_iobase, ARIADSP_NPORT, 0, &ioh)) {
    260 		DPRINTF(("aria: aria probe failed\n"));
    261 		return 0;
    262 	}
    263 
    264 	if (cf->cf_flags & 1)
    265 		aria_prometheus_kludge(ia, ioh);
    266 
    267 	if (ariareset(ia->ia_iot, ioh) != 0) {
    268 		DPRINTF(("aria: aria probe failed\n"));
    269 		bus_space_unmap(ia->ia_iot, ioh,  ARIADSP_NPORT);
    270 		return 0;
    271 	}
    272 
    273 	bus_space_unmap(ia->ia_iot, ioh,  ARIADSP_NPORT);
    274 
    275 	ia->ia_iosize = ARIADSP_NPORT;
    276 	DPRINTF(("aria: aria probe succeeded\n"));
    277 	return 1;
    278 }
    279 
    280 /*
    281  * I didn't call this a kludge for
    282  * nothing.  This is cribbed from
    283  * ariainit, the author of that
    284  * disassembled some code to discover
    285  * how to set up the initial values of
    286  * the card.  Without this, the card
    287  * is dead. (It will not respond to _any_
    288  * input at all.)
    289  *
    290  * ariainit can be found (ftp) at:
    291  * ftp://ftp.wi.leidenuniv.nl/pub/audio/aria/programming/contrib/ariainit.zip
    292  * currently.
    293  */
    294 
    295 void
    296 aria_prometheus_kludge(ia, ioh1)
    297 	struct isa_attach_args *ia;
    298 	bus_space_handle_t ioh1;
    299 {
    300 	bus_space_tag_t iot;
    301 	bus_space_handle_t ioh;
    302 	u_short	end;
    303 
    304 	DPRINTF(("aria: begin aria_prometheus_kludge\n"));
    305 
    306 /* Begin Config Sequence */
    307 
    308 	iot = ia->ia_iot;
    309 	bus_space_map(iot, 0x200, 8, 0, &ioh);
    310 
    311         bus_space_write_1(iot, ioh, 4, 0x4c);
    312         bus_space_write_1(iot, ioh, 5, 0x42);
    313         bus_space_write_1(iot, ioh, 6, 0x00);
    314         bus_space_write_2(iot, ioh, 0, 0x0f);
    315         bus_space_write_1(iot, ioh, 1, 0x00);
    316         bus_space_write_2(iot, ioh, 0, 0x02);
    317         bus_space_write_1(iot, ioh, 1, ia->ia_iobase>>2);
    318 
    319 /*
    320  * These next three lines set up the iobase
    321  * and the irq; and disable the drq.
    322  */
    323 
    324 	aria_do_kludge(iot, ioh, ioh1, 0x111, ((ia->ia_iobase-0x280)>>2)+0xA0,
    325 		       0xbf, 0xa0);
    326 	aria_do_kludge(iot, ioh, ioh1, 0x011, ia->ia_irq-6, 0xf8, 0x00);
    327 	aria_do_kludge(iot, ioh, ioh1, 0x011, 0x00, 0xef, 0x00);
    328 
    329 /* The rest of these lines just disable everything else */
    330 
    331 	aria_do_kludge(iot, ioh, ioh1, 0x113, 0x00, 0x88, 0x00);
    332 	aria_do_kludge(iot, ioh, ioh1, 0x013, 0x00, 0xf8, 0x00);
    333 	aria_do_kludge(iot, ioh, ioh1, 0x013, 0x00, 0xef, 0x00);
    334 	aria_do_kludge(iot, ioh, ioh1, 0x117, 0x00, 0x88, 0x00);
    335 	aria_do_kludge(iot, ioh, ioh1, 0x017, 0x00, 0xff, 0x00);
    336 
    337 /* End Sequence */
    338 
    339 	bus_space_write_1(iot, ioh, 0, 0x0f);
    340 	end = bus_space_read_1(iot, ioh1, 0);
    341 	bus_space_write_2(iot, ioh, 0, 0x0f);
    342 	bus_space_write_1(iot, ioh, 1, end|0x80);
    343 	bus_space_read_1(iot, ioh, 0);
    344 
    345 	bus_space_unmap(iot, ioh, 8);
    346 /*
    347  * This delay is necessary for some reason,
    348  * at least it would crash, and sometimes not
    349  * probe properly if it did not exist.
    350  */
    351 	delay(1000000);
    352 }
    353 
    354 void
    355 aria_do_kludge(iot, ioh, ioh1, func, bits, and, or)
    356 	bus_space_tag_t iot;
    357 	bus_space_handle_t ioh;
    358 	bus_space_handle_t ioh1;
    359 	u_short func;
    360 	u_short bits;
    361 	u_short and;
    362 	u_short or;
    363 {
    364 	u_int i;
    365 	if (func & 0x100) {
    366 		func &= ~0x100;
    367 		if (bits) {
    368 			bus_space_write_2(iot, ioh, 0, func-1);
    369 			bus_space_write_1(iot, ioh, 1, bits);
    370 		}
    371 	} else
    372 		or |= bits;
    373 
    374 	bus_space_write_1(iot, ioh, 0, func);
    375 	i = bus_space_read_1(iot, ioh1, 0);
    376 	bus_space_write_2(iot, ioh, 0, func);
    377 	bus_space_write_1(iot, ioh, 1, (i&and) | or);
    378 }
    379 
    380 /*
    381  * Attach hardware to driver, attach hardware driver to audio
    382  * pseudo-device driver.
    383  */
    384 void
    385 ariaattach(parent, self, aux)
    386 	struct device *parent, *self;
    387 	void *aux;
    388 {
    389 	bus_space_handle_t ioh;
    390 	struct aria_softc *sc = (void *)self;
    391 	struct isa_attach_args *ia = aux;
    392 	u_short i;
    393 
    394 	if (bus_space_map(ia->ia_iot, ia->ia_iobase, ARIADSP_NPORT, 0, &ioh))
    395 		panic("%s: can map io port range", self->dv_xname);
    396 
    397 	sc->sc_iot = ia->ia_iot;
    398 	sc->sc_ioh = ioh;
    399 	sc->sc_ic = ia->ia_ic;
    400 
    401 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
    402 			IPL_AUDIO, aria_intr, sc);
    403 
    404 	DPRINTF(("isa_intr_establish() returns (%x)\n", (unsigned) sc->sc_ih));
    405 
    406 	i = aria_getdspmem(sc, ARIAA_HARDWARE_A);
    407 
    408 	sc->sc_hardware  = 0;
    409 	sc->sc_hardware |= ((i>>13)&0x01)==1 ? ARIA_TELEPHONE:0;
    410 	sc->sc_hardware |= (((i>>5)&0x07))==0x04 ? ARIA_MIXER:0;
    411 	sc->sc_hardware |= (aria_getdspmem(sc, ARIAA_MODEL_A)>=1)?ARIA_MODEL:0;
    412 
    413 	sc->sc_open       = 0;
    414 	sc->sc_play       = 0;
    415 	sc->sc_record     = 0;
    416 	sc->sc_rate       = 7875;
    417 	sc->sc_chans      = 1;
    418 	sc->sc_blocksize  = 1024;
    419 	sc->sc_precision  = 8;
    420         sc->sc_rintr      = 0;
    421         sc->sc_rarg       = 0;
    422         sc->sc_pintr      = 0;
    423         sc->sc_parg       = 0;
    424 	sc->sc_gain[0]       = 127;
    425 	sc->sc_gain[1]       = 127;
    426 
    427 	for (i=0; i<6; i++) {
    428 		if (i == ARIAMIX_TEL_LVL)
    429 			sc->aria_mix[i].num_channels = 1;
    430 		else
    431 			sc->aria_mix[i].num_channels = 2;
    432 		sc->aria_mix[i].level[0] = 127;
    433 		sc->aria_mix[i].level[1] = 127;
    434 	}
    435 
    436 	sc->ariamix_master.num_channels = 2;
    437 	sc->ariamix_master.level[0] = 222;
    438 	sc->ariamix_master.level[1] = 222;
    439 	sc->ariamix_master.bass[0] = 127;
    440 	sc->ariamix_master.bass[1] = 127;
    441 	sc->ariamix_master.treble[0] = 127;
    442 	sc->ariamix_master.treble[1] = 127;
    443 	sc->aria_mix_source = 0;
    444 
    445 	aria_commit_settings(sc);
    446 
    447 	printf(": dsp %s", (ARIA_MODEL&sc->sc_hardware)?"SC18026":"SC18025");
    448 	if (ARIA_TELEPHONE&sc->sc_hardware)
    449 		printf(", tel");
    450 	if (ARIA_MIXER&sc->sc_hardware)
    451 		printf(", SC18075 mixer");
    452 	printf("\n");
    453 
    454 	sprintf(aria_device.version, "%s",
    455 		ARIA_MODEL & sc->sc_hardware ? "SC18026" : "SC18025");
    456 
    457 	audio_attach_mi(&aria_hw_if, (void *)sc, &sc->sc_dev);
    458 }
    459 
    460 /*
    461  * Various routines to interface to higher level audio driver
    462  */
    463 
    464 int
    465 ariaopen(addr, flags)
    466 	void *addr;
    467 	int flags;
    468 {
    469 	struct aria_softc *sc = addr;
    470 
    471 	DPRINTF(("ariaopen() called\n"));
    472 
    473 	if (!sc)
    474 		return ENXIO;
    475 	if ((flags&FREAD) && (sc->sc_open & ARIAR_OPEN_RECORD))
    476 		return ENXIO;
    477 	if ((flags&FWRITE) && (sc->sc_open & ARIAR_OPEN_PLAY))
    478 		return ENXIO;
    479 
    480 	if (flags&FREAD)
    481 		sc->sc_open |= ARIAR_OPEN_RECORD;
    482 	if (flags&FWRITE)
    483 		sc->sc_open |= ARIAR_OPEN_PLAY;
    484 	sc->sc_play  = 0;
    485 	sc->sc_record= 0;
    486 	sc->sc_rintr = 0;
    487 	sc->sc_rarg  = 0;
    488 	sc->sc_pintr = 0;
    489 	sc->sc_parg  = 0;
    490 
    491 	return 0;
    492 }
    493 
    494 int
    495 aria_getdev(addr, retp)
    496 	void *addr;
    497 	struct audio_device *retp;
    498 {
    499 	*retp = aria_device;
    500 	return 0;
    501 }
    502 
    503 /*
    504  * Various routines to interface to higher level audio driver
    505  */
    506 
    507 int
    508 aria_query_encoding(addr, fp)
    509     void *addr;
    510     struct audio_encoding *fp;
    511 {
    512 	struct aria_softc *sc = addr;
    513 
    514 	switch (fp->index) {
    515 		case 0:
    516 			strcpy(fp->name, AudioEmulaw);
    517 			fp->encoding = AUDIO_ENCODING_ULAW;
    518 			fp->precision = 8;
    519 			if ((ARIA_MODEL&sc->sc_hardware) == 0)
    520 				fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    521 			break;
    522 		case 1:
    523 			strcpy(fp->name, AudioEalaw);
    524 			fp->encoding = AUDIO_ENCODING_ALAW;
    525 			fp->precision = 8;
    526 			if ((ARIA_MODEL&sc->sc_hardware) == 0)
    527 				fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    528 			break;
    529 		case 2:
    530 			strcpy(fp->name, AudioEslinear);
    531 			fp->encoding = AUDIO_ENCODING_SLINEAR;
    532 			fp->precision = 8;
    533 			fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    534 			break;
    535 		case 3:
    536 			strcpy(fp->name, AudioEslinear_le);
    537 			fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
    538 			fp->precision = 16;
    539 			fp->flags = 0;
    540 			break;
    541 		case 4:
    542 			strcpy(fp->name, AudioEslinear_be);
    543 			fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
    544 			fp->precision = 16;
    545 			fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    546 			break;
    547 		case 5:
    548 			strcpy(fp->name, AudioEulinear);
    549 			fp->encoding = AUDIO_ENCODING_ULINEAR;
    550 			fp->precision = 8;
    551 			fp->flags = 0;
    552 			break;
    553 		case 6:
    554 			strcpy(fp->name, AudioEulinear_le);
    555 			fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
    556 			fp->precision = 16;
    557 			fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    558 			break;
    559 		case 7:
    560 			strcpy(fp->name, AudioEulinear_be);
    561 			fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
    562 			fp->precision = 16;
    563 			fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    564 			break;
    565 		default:
    566 			return(EINVAL);
    567 		/*NOTREACHED*/
    568 	}
    569 
    570 	return (0);
    571 }
    572 
    573 /*
    574  * Store blocksize in bytes.
    575  */
    576 
    577 int
    578 aria_round_blocksize(addr, blk)
    579 	void *addr;
    580 	int blk;
    581 {
    582 	int i;
    583 #if 0 /* XXX -- this is being a tad bit of a problem... */
    584 	for (i=64; i<1024; i*=2)
    585 		if (blk <= i)
    586 			break;
    587 #else
    588 	i = 1024;
    589 #endif
    590 	return(i);
    591 }
    592 
    593 int
    594 aria_get_props(addr)
    595 	void *addr;
    596 {
    597 	return AUDIO_PROP_FULLDUPLEX;
    598 }
    599 
    600 int
    601 aria_set_params(addr, setmode, usemode, p, r)
    602 	void *addr;
    603 	int setmode, usemode;
    604 	struct audio_params *p, *r;
    605 {
    606 	struct aria_softc *sc = addr;
    607 
    608 	switch(p->encoding) {
    609 	case AUDIO_ENCODING_ULAW:
    610 	case AUDIO_ENCODING_ALAW:
    611 	case AUDIO_ENCODING_SLINEAR:
    612 	case AUDIO_ENCODING_SLINEAR_LE:
    613 	case AUDIO_ENCODING_SLINEAR_BE:
    614 	case AUDIO_ENCODING_ULINEAR:
    615 	case AUDIO_ENCODING_ULINEAR_LE:
    616 	case AUDIO_ENCODING_ULINEAR_BE:
    617 		break;
    618 	default:
    619 		return (EINVAL);
    620 	}
    621 
    622 	if (p->sample_rate <= 9450)
    623 		p->sample_rate = 7875;
    624 	else if (p->sample_rate <= 13387)
    625 		p->sample_rate = 11025;
    626 	else if (p->sample_rate <= 18900)
    627 		p->sample_rate = 15750;
    628 	else if (p->sample_rate <= 26775)
    629 		p->sample_rate = 22050;
    630 	else if (p->sample_rate <= 37800)
    631 		p->sample_rate = 31500;
    632 	else
    633 		p->sample_rate = 44100;
    634 
    635 	sc->sc_encoding = p->encoding;
    636 	sc->sc_precision = p->precision;
    637 	sc->sc_chans = p->channels;
    638 	sc->sc_rate = p->sample_rate;
    639 
    640 	switch(p->encoding) {
    641 	case AUDIO_ENCODING_ULAW:
    642 		if ((ARIA_MODEL&sc->sc_hardware) == 0) {
    643 			p->sw_code = mulaw_to_ulinear8;
    644 			r->sw_code = ulinear8_to_mulaw;
    645 		}
    646 		break;
    647 	case AUDIO_ENCODING_ALAW:
    648 		if ((ARIA_MODEL&sc->sc_hardware) == 0) {
    649 			p->sw_code = alaw_to_ulinear8;
    650 			r->sw_code = ulinear8_to_alaw;
    651 		}
    652 		break;
    653 	case AUDIO_ENCODING_SLINEAR:
    654 		p->sw_code = r->sw_code = change_sign8;
    655 		break;
    656 	case AUDIO_ENCODING_ULINEAR_LE:
    657 		p->sw_code = r->sw_code = change_sign16_le;
    658 		break;
    659 	case AUDIO_ENCODING_SLINEAR_BE:
    660 		p->sw_code = r->sw_code = swap_bytes;
    661 		break;
    662 	case AUDIO_ENCODING_ULINEAR_BE:
    663 		p->sw_code = r->sw_code = swap_bytes_change_sign16_le;
    664 		break;
    665 	}
    666 
    667 	return 0;
    668 }
    669 
    670 /*
    671  * This is where all of the twiddling goes on.
    672  */
    673 
    674 int
    675 aria_commit_settings(addr)
    676 	void *addr;
    677 {
    678         struct aria_softc *sc = addr;
    679 	bus_space_tag_t iot = sc->sc_iot;
    680 	bus_space_handle_t ioh = sc->sc_ioh;
    681 	static u_char tones[16] =
    682 	    { 7, 6, 5, 4, 3, 2, 1, 0, 8, 9, 10, 11, 12, 13, 14, 15 };
    683 	u_short format;
    684 	u_short left, right;
    685 	u_short samp;
    686 	u_char i;
    687 
    688 	DPRINTF(("aria_commit_settings\n"));
    689 
    690 	switch (sc->sc_rate) {
    691 	case  7875: format = 0x00; samp = 0x60; break;
    692 	case 11025: format = 0x00; samp = 0x40; break;
    693 	case 15750: format = 0x10; samp = 0x60; break;
    694 	case 22050: format = 0x10; samp = 0x40; break;
    695 	case 31500: format = 0x10; samp = 0x20; break;
    696 	case 44100: format = 0x20; samp = 0x00; break;
    697 	default:    format = 0x00; samp = 0x40; break;/* XXX can we get here? */
    698 	}
    699 
    700 	if ((ARIA_MODEL&sc->sc_hardware) != 0) {
    701 		format |= (sc->sc_encoding==AUDIO_ENCODING_ULAW)?0x06:0x00;
    702 		format |= (sc->sc_encoding==AUDIO_ENCODING_ALAW)?0x08:0x00;
    703 	}
    704 
    705 	format |= (sc->sc_precision==16)?0x02:0x00;
    706 	format |= (sc->sc_chans==2)?1:0;
    707 	samp |= bus_space_read_2(iot, ioh, ARIADSP_STATUS) & ~0x60;
    708 
    709 	aria_sendcmd(sc, ARIADSPC_FORMAT, format, -1, -1);
    710 	bus_space_write_2(iot, ioh, ARIADSP_CONTROL, samp);
    711 
    712 	if (sc->sc_hardware&ARIA_MIXER) {
    713 		for (i = 0; i < 6; i++)
    714 			aria_set_mixer(sc, i);
    715 
    716 		if (sc->sc_chans==2) {
    717 			aria_sendcmd(sc, ARIADSPC_CHAN_VOL, ARIAR_PLAY_CHAN,
    718 				     ((sc->sc_gain[0]+sc->sc_gain[1])/2)<<7,
    719 				     -1);
    720 			aria_sendcmd(sc, ARIADSPC_CHAN_PAN, ARIAR_PLAY_CHAN,
    721 				     (sc->sc_gain[0]-sc->sc_gain[1])/4+0x40,
    722 				     -1);
    723 		} else {
    724 			aria_sendcmd(sc, ARIADSPC_CHAN_VOL, ARIAR_PLAY_CHAN,
    725 				     sc->sc_gain[0]<<7, -1);
    726 			aria_sendcmd(sc, ARIADSPC_CHAN_PAN, ARIAR_PLAY_CHAN,
    727 				     0x40, -1);
    728 		}
    729 
    730 		aria_sendcmd(sc, ARIADSPC_MASMONMODE,
    731 			     sc->ariamix_master.num_channels != 2, -1, -1);
    732 
    733 		aria_sendcmd(sc, ARIADSPC_MIXERVOL, 0x0004,
    734 			     sc->ariamix_master.level[0] << 7,
    735 			     sc->ariamix_master.level[1] << 7);
    736 
    737 		/* Convert treble/bass from byte to soundcard style */
    738 
    739 		left  = (tones[(sc->ariamix_master.treble[0]>>4)&0x0f]<<8) |
    740 			 tones[(sc->ariamix_master.bass[0]>>4)&0x0f];
    741 		right = (tones[(sc->ariamix_master.treble[1]>>4)&0x0f]<<8) |
    742 			 tones[(sc->ariamix_master.bass[1]>>4)&0x0f];
    743 
    744 		aria_sendcmd(sc, ARIADSPC_TONE, left, right, -1);
    745 	}
    746 
    747 	aria_sendcmd(sc, ARIADSPC_BLOCKSIZE, sc->sc_blocksize/2, -1, -1);
    748 
    749 /*
    750  * If we think that the card is recording or playing, start it up again here.
    751  * Some of the previous commands turn the channels off.
    752  */
    753 
    754 	if (sc->sc_record&(1<<ARIAR_RECORD_CHAN))
    755 		aria_sendcmd(sc, ARIADSPC_START_REC, ARIAR_RECORD_CHAN, -1,-1);
    756 
    757 	if (sc->sc_play&(1<<ARIAR_PLAY_CHAN))
    758 		aria_sendcmd(sc, ARIADSPC_START_PLAY, ARIAR_PLAY_CHAN, -1, -1);
    759 
    760 	return(0);
    761 }
    762 
    763 void
    764 aria_set_mixer(sc, i)
    765         struct aria_softc *sc;
    766 	int i;
    767 {
    768 	u_char source;
    769 	switch(i) {
    770 	case ARIAMIX_MIC_LVL:     source = 0x0001; break;
    771 	case ARIAMIX_CD_LVL:      source = 0x0002; break;
    772 	case ARIAMIX_LINE_IN_LVL: source = 0x0008; break;
    773 	case ARIAMIX_TEL_LVL:     source = 0x0020; break;
    774 	case ARIAMIX_AUX_LVL:     source = 0x0010; break;
    775 	case ARIAMIX_DAC_LVL:     source = 0x0004; break;
    776 	default:                  source = 0x0000; break;
    777 	}
    778 
    779 	if (source != 0x0000 && source != 0x0004) {
    780 		if (sc->aria_mix[i].mute == 1)
    781 			aria_sendcmd(sc, ARIADSPC_INPMONMODE, source, 3, -1);
    782 		else
    783 			aria_sendcmd(sc, ARIADSPC_INPMONMODE, source,
    784 				     sc->aria_mix[i].num_channels != 2, -1);
    785 
    786 		aria_sendcmd(sc, ARIADSPC_INPMONMODE, 0x8000|source,
    787 			     sc->aria_mix[i].num_channels != 2, -1);
    788 		aria_sendcmd(sc, ARIADSPC_MIXERVOL, source,
    789 			     sc->aria_mix[i].level[0] << 7,
    790 			     sc->aria_mix[i].level[1] << 7);
    791 	}
    792 
    793 	if (sc->aria_mix_source == i) {
    794 		aria_sendcmd(sc, ARIADSPC_ADCSOURCE, source, -1, -1);
    795 
    796 		if (sc->sc_open & ARIAR_OPEN_RECORD)
    797 			aria_sendcmd(sc, ARIADSPC_ADCCONTROL, 1, -1, -1);
    798 		else
    799 			aria_sendcmd(sc, ARIADSPC_ADCCONTROL, 0, -1, -1);
    800 	}
    801 }
    802 
    803 void
    804 ariaclose(addr)
    805 	void *addr;
    806 {
    807         struct aria_softc *sc = addr;
    808 
    809 	DPRINTF(("aria_close sc=0x%x\n", (unsigned) sc));
    810 
    811         sc->sc_rintr = 0;
    812         sc->sc_pintr = 0;
    813 	sc->sc_rdiobuffer = 0;
    814 	sc->sc_pdiobuffer = 0;
    815 
    816 	if (sc->sc_play&(1<<ARIAR_PLAY_CHAN) &&
    817 	    sc->sc_open & ARIAR_OPEN_PLAY) {
    818 		aria_sendcmd(sc, ARIADSPC_STOP_PLAY, ARIAR_PLAY_CHAN, -1, -1);
    819 		sc->sc_play &= ~(1<<ARIAR_PLAY_CHAN);
    820 	}
    821 
    822 	if (sc->sc_record&(1<<ARIAR_RECORD_CHAN) &&
    823 	    sc->sc_open & ARIAR_OPEN_RECORD) {
    824 		aria_sendcmd(sc, ARIADSPC_STOP_REC, ARIAR_RECORD_CHAN, -1, -1);
    825 		sc->sc_record &= ~(1<<ARIAR_RECORD_CHAN);
    826 	}
    827 
    828 	sc->sc_open = 0;
    829 
    830 	if (aria_reset(sc) != 0) {
    831 		delay(500);
    832 		aria_reset(sc);
    833 	}
    834 }
    835 
    836 /*
    837  * Reset the hardware.
    838  */
    839 
    840 int ariareset(iot, ioh)
    841 	bus_space_tag_t iot;
    842 	bus_space_handle_t ioh;
    843 {
    844 	struct aria_softc tmp, *sc = &tmp;
    845 
    846 	sc->sc_iot = iot;
    847 	sc->sc_ioh = ioh;
    848 	return aria_reset(sc);
    849 }
    850 
    851 int
    852 aria_reset(sc)
    853 	struct aria_softc *sc;
    854 {
    855 	bus_space_tag_t iot = sc->sc_iot;
    856 	bus_space_handle_t ioh = sc->sc_ioh;
    857 	int fail=0;
    858 	int i;
    859 
    860 	bus_space_write_2(iot, ioh, ARIADSP_CONTROL,
    861 			  ARIAR_ARIA_SYNTH | ARIAR_SR22K|ARIAR_DSPINTWR);
    862 	aria_putdspmem(sc, 0x6102, 0);
    863 
    864 	fail |= aria_sendcmd(sc, ARIADSPC_SYSINIT, 0x0000, 0x0000, 0x0000);
    865 
    866 	for (i=0; i < ARIAR_NPOLL; i++)
    867 		if (aria_getdspmem(sc, ARIAA_TASK_A) == 1)
    868 			break;
    869 
    870 	bus_space_write_2(iot, ioh, ARIADSP_CONTROL,
    871 			  ARIAR_ARIA_SYNTH|ARIAR_SR22K | ARIAR_DSPINTWR |
    872 			  ARIAR_PCINTWR);
    873 	fail |= aria_sendcmd(sc, ARIADSPC_MODE, ARIAV_MODE_NO_SYNTH,-1,-1);
    874 
    875 	return (fail);
    876 }
    877 
    878 /*
    879  * Lower-level routines
    880  */
    881 
    882 void
    883 aria_putdspmem(sc, loc, val)
    884 	struct aria_softc *sc;
    885 	u_short loc;
    886 	u_short val;
    887 {
    888 	bus_space_tag_t iot = sc->sc_iot;
    889 	bus_space_handle_t ioh = sc->sc_ioh;
    890 	bus_space_write_2(iot, ioh, ARIADSP_DMAADDRESS, loc);
    891 	bus_space_write_2(iot, ioh, ARIADSP_DMADATA, val);
    892 }
    893 
    894 u_short
    895 aria_getdspmem(sc, loc)
    896 	struct aria_softc *sc;
    897 	u_short loc;
    898 {
    899 	bus_space_tag_t iot = sc->sc_iot;
    900 	bus_space_handle_t ioh = sc->sc_ioh;
    901 	bus_space_write_2(iot, ioh, ARIADSP_DMAADDRESS, loc);
    902 	return bus_space_read_2(iot, ioh, ARIADSP_DMADATA);
    903 }
    904 
    905 /*
    906  * aria_sendcmd()
    907  *  each full DSP command is unified into this
    908  *  function.
    909  */
    910 
    911 #define ARIASEND(data, flag) \
    912 	for (i = ARIAR_NPOLL; \
    913 	     (bus_space_read_2(iot, ioh, ARIADSP_STATUS) & ARIAR_BUSY) && i>0; \
    914 	     i--) \
    915 		; \
    916 	if (bus_space_read_2(iot, ioh, ARIADSP_STATUS) & ARIAR_BUSY) \
    917 		fail |= flag; \
    918 	bus_space_write_2(iot, ioh, ARIADSP_WRITE, (u_short)data)
    919 
    920 int
    921 aria_sendcmd(sc, command, arg1, arg2, arg3)
    922 	struct aria_softc *sc;
    923 	u_short command;
    924 	int arg1;
    925 	int arg2;
    926 	int arg3;
    927 {
    928 	bus_space_tag_t iot = sc->sc_iot;
    929 	bus_space_handle_t ioh = sc->sc_ioh;
    930 	int i, fail = 0;
    931 
    932 	ARIASEND(command, 1);
    933 	if (arg1 != -1) {
    934 		ARIASEND(arg1, 2);
    935 	}
    936 	if (arg2 != -1) {
    937 		ARIASEND(arg2, 4);
    938 	}
    939 	if (arg3 != -1) {
    940 		ARIASEND(arg3, 8);
    941 	}
    942 	ARIASEND(ARIADSPC_TERM, 16);
    943 
    944 	if (fail) {
    945 		sc->sc_sendcmd_err++;
    946 #ifdef AUDIO_DEBUG
    947 		DPRINTF(("aria_sendcmd: failure=(%d) cmd=(0x%x) fail=(0x%x)\n",
    948 			 sc->sc_sendcmd_err, command, fail));
    949 #endif
    950 		return -1;
    951 	}
    952 
    953 	return 0;
    954 }
    955 #undef ARIASEND
    956 
    957 int
    958 aria_halt_input(addr)
    959 	void *addr;
    960 {
    961 	struct aria_softc *sc = addr;
    962 
    963 	DPRINTF(("aria_halt_input\n"));
    964 
    965 	if (sc->sc_record & (1<<0)) {
    966 		aria_sendcmd(sc, ARIADSPC_STOP_REC, 0, -1, -1);
    967 		sc->sc_record &= ~(1<<0);
    968 	}
    969 
    970 	return(0);
    971 }
    972 
    973 int
    974 aria_halt_output(addr)
    975 	void *addr;
    976 {
    977 	struct aria_softc *sc = addr;
    978 
    979 	DPRINTF(("aria_halt_output\n"));
    980 
    981 	if (sc->sc_play & (1<<1)) {
    982 		aria_sendcmd(sc, ARIADSPC_STOP_PLAY, 1, -1, -1);
    983 		sc->sc_play &= ~(1<<1);
    984 	}
    985 
    986 	return(0);
    987 }
    988 
    989 /*
    990  * Here we just set up the buffers.  If we receive
    991  * an interrupt without these set, it is ignored.
    992  */
    993 
    994 int
    995 aria_start_input(addr, p, cc, intr, arg)
    996 	void *addr;
    997 	void *p;
    998 	int cc;
    999 	void (*intr) __P((void *));
   1000 	void *arg;
   1001 {
   1002 	struct aria_softc *sc = addr;
   1003 
   1004 	DPRINTF(("aria_start_input %d @ %x\n", cc, (unsigned) p));
   1005 
   1006 	if (cc != sc->sc_blocksize) {
   1007 		DPRINTF(("aria_start_input reqsize %d not sc_blocksize %d\n",
   1008 			cc, sc->sc_blocksize));
   1009 		return EINVAL;
   1010 	}
   1011 
   1012 	sc->sc_rarg = arg;
   1013 	sc->sc_rintr = intr;
   1014 	sc->sc_rdiobuffer = p;
   1015 
   1016 	if (!(sc->sc_record&(1<<ARIAR_RECORD_CHAN))) {
   1017 		aria_sendcmd(sc, ARIADSPC_START_REC, ARIAR_RECORD_CHAN, -1,-1);
   1018 		sc->sc_record |= (1<<ARIAR_RECORD_CHAN);
   1019 	}
   1020 
   1021 	return 0;
   1022 }
   1023 
   1024 int
   1025 aria_start_output(addr, p, cc, intr, arg)
   1026 	void *addr;
   1027 	void *p;
   1028 	int cc;
   1029 	void (*intr) __P((void *));
   1030 	void *arg;
   1031 {
   1032 	struct aria_softc *sc = addr;
   1033 
   1034 	DPRINTF(("aria_start_output %d @ %x\n", cc, (unsigned) p));
   1035 
   1036 	if (cc != sc->sc_blocksize) {
   1037 		DPRINTF(("aria_start_output reqsize %d not sc_blocksize %d\n",
   1038 			cc, sc->sc_blocksize));
   1039 		return EINVAL;
   1040 	}
   1041 
   1042 	sc->sc_parg = arg;
   1043 	sc->sc_pintr = intr;
   1044 	sc->sc_pdiobuffer = p;
   1045 
   1046 	if (!(sc->sc_play&(1<<ARIAR_PLAY_CHAN))) {
   1047 		aria_sendcmd(sc, ARIADSPC_START_PLAY, ARIAR_PLAY_CHAN, -1, -1);
   1048 		sc->sc_play |= (1<<ARIAR_PLAY_CHAN);
   1049 	}
   1050 
   1051 	return 0;
   1052 }
   1053 
   1054 /*
   1055  * Process an interrupt.  This should be a
   1056  * request (from the card) to write or read
   1057  * samples.
   1058  */
   1059 int
   1060 aria_intr(arg)
   1061 	void *arg;
   1062 {
   1063 	struct  aria_softc *sc = arg;
   1064 	bus_space_tag_t iot = sc->sc_iot;
   1065 	bus_space_handle_t ioh = sc->sc_ioh;
   1066 	u_short *pdata = sc->sc_pdiobuffer;
   1067 	u_short *rdata = sc->sc_rdiobuffer;
   1068 	u_short address;
   1069 
   1070 #if 0 /*  XXX --  BAD BAD BAD (That this is #define'd out */
   1071 	DPRINTF(("Checking to see if this is our intr\n"));
   1072 
   1073 	if ((inw(iobase) & 1) != 0x1)
   1074 		return 0;  /* not for us */
   1075 #endif
   1076 
   1077 	sc->sc_interrupts++;
   1078 
   1079 	DPRINTF(("aria_intr\n"));
   1080 
   1081 	if ((sc->sc_open & ARIAR_OPEN_PLAY) && (pdata!=NULL)) {
   1082 		DPRINTF(("aria_intr play=(%x)\n", (unsigned) pdata));
   1083 		address = 0x8000 - 2*(sc->sc_blocksize);
   1084 		address+= aria_getdspmem(sc, ARIAA_PLAY_FIFO_A);
   1085 		bus_space_write_2(iot, ioh, ARIADSP_DMAADDRESS, address);
   1086 		bus_space_write_multi_2(iot, ioh, ARIADSP_DMADATA, pdata,
   1087 					sc->sc_blocksize / 2);
   1088 		if (sc->sc_pintr != NULL)
   1089 			(*sc->sc_pintr)(sc->sc_parg);
   1090 	}
   1091 
   1092 	if ((sc->sc_open & ARIAR_OPEN_RECORD) && (rdata!=NULL)) {
   1093 		DPRINTF(("aria_intr record=(%x)\n", (unsigned) rdata));
   1094 		address = 0x8000 - (sc->sc_blocksize);
   1095 		address+= aria_getdspmem(sc, ARIAA_REC_FIFO_A);
   1096 		bus_space_write_2(iot, ioh, ARIADSP_DMAADDRESS, address);
   1097 		bus_space_read_multi_2(iot, ioh, ARIADSP_DMADATA, rdata,
   1098 				       sc->sc_blocksize / 2);
   1099 		if (sc->sc_rintr != NULL)
   1100 			(*sc->sc_rintr)(sc->sc_rarg);
   1101 	}
   1102 
   1103 	aria_sendcmd(sc, ARIADSPC_TRANSCOMPLETE, -1, -1, -1);
   1104 
   1105 	return 1;
   1106 }
   1107 
   1108 int
   1109 aria_mixer_set_port(addr, cp)
   1110 	void *addr;
   1111 	mixer_ctrl_t *cp;
   1112 {
   1113 	struct aria_softc *sc = addr;
   1114 	int error = EINVAL;
   1115 
   1116 	DPRINTF(("aria_mixer_set_port\n"));
   1117 
   1118 	/* This could be done better, no mixer still has some controls. */
   1119 	if (!(ARIA_MIXER & sc->sc_hardware))
   1120 		return ENXIO;
   1121 
   1122 	if (cp->type == AUDIO_MIXER_VALUE) {
   1123 		mixer_level_t *mv = &cp->un.value;
   1124 		switch (cp->dev) {
   1125 		case ARIAMIX_MIC_LVL:
   1126 			if (mv->num_channels == 1 || mv->num_channels == 2) {
   1127 				sc->aria_mix[ARIAMIX_MIC_LVL].num_channels =
   1128 					mv->num_channels;
   1129 				sc->aria_mix[ARIAMIX_MIC_LVL].level[0] =
   1130 					mv->level[0];
   1131 				sc->aria_mix[ARIAMIX_MIC_LVL].level[1] =
   1132 					mv->level[1];
   1133 				error = 0;
   1134 			}
   1135 			break;
   1136 
   1137 		case ARIAMIX_LINE_IN_LVL:
   1138 			if (mv->num_channels == 1 || mv->num_channels == 2) {
   1139 				sc->aria_mix[ARIAMIX_LINE_IN_LVL].num_channels=
   1140 					mv->num_channels;
   1141 				sc->aria_mix[ARIAMIX_LINE_IN_LVL].level[0] =
   1142 					mv->level[0];
   1143 				sc->aria_mix[ARIAMIX_LINE_IN_LVL].level[1] =
   1144 					mv->level[1];
   1145 				error = 0;
   1146 			}
   1147 			break;
   1148 
   1149 		case ARIAMIX_CD_LVL:
   1150 			if (mv->num_channels == 1 || mv->num_channels == 2) {
   1151 				sc->aria_mix[ARIAMIX_CD_LVL].num_channels =
   1152 					mv->num_channels;
   1153 				sc->aria_mix[ARIAMIX_CD_LVL].level[0] =
   1154 					mv->level[0];
   1155 				sc->aria_mix[ARIAMIX_CD_LVL].level[1] =
   1156 					mv->level[1];
   1157 				error = 0;
   1158 			}
   1159 			break;
   1160 
   1161 		case ARIAMIX_TEL_LVL:
   1162 			if (mv->num_channels == 1) {
   1163 				sc->aria_mix[ARIAMIX_TEL_LVL].num_channels =
   1164 					mv->num_channels;
   1165 				sc->aria_mix[ARIAMIX_TEL_LVL].level[0] =
   1166 					mv->level[0];
   1167 				error = 0;
   1168 			}
   1169 			break;
   1170 
   1171 		case ARIAMIX_DAC_LVL:
   1172 			if (mv->num_channels == 1 || mv->num_channels == 2) {
   1173 				sc->aria_mix[ARIAMIX_DAC_LVL].num_channels =
   1174 					mv->num_channels;
   1175 				sc->aria_mix[ARIAMIX_DAC_LVL].level[0] =
   1176 					mv->level[0];
   1177 				sc->aria_mix[ARIAMIX_DAC_LVL].level[1] =
   1178 					mv->level[1];
   1179 				error = 0;
   1180 			}
   1181 			break;
   1182 
   1183 		case ARIAMIX_AUX_LVL:
   1184 			if (mv->num_channels == 1 || mv->num_channels == 2) {
   1185 				sc->aria_mix[ARIAMIX_AUX_LVL].num_channels =
   1186 					mv->num_channels;
   1187 				sc->aria_mix[ARIAMIX_AUX_LVL].level[0] =
   1188 					mv->level[0];
   1189 				sc->aria_mix[ARIAMIX_AUX_LVL].level[1] =
   1190 					mv->level[1];
   1191 				error = 0;
   1192 			}
   1193 			break;
   1194 
   1195 		case ARIAMIX_MASTER_LVL:
   1196 			if (mv->num_channels == 1 || mv->num_channels == 2) {
   1197 				sc->ariamix_master.num_channels =
   1198 					mv->num_channels;
   1199 				sc->ariamix_master.level[0] = mv->level[0];
   1200 				sc->ariamix_master.level[1] = mv->level[1];
   1201 				error = 0;
   1202 			}
   1203 			break;
   1204 
   1205 		case ARIAMIX_MASTER_TREBLE:
   1206 			if (mv->num_channels == 2) {
   1207 				sc->ariamix_master.treble[0] =
   1208 					mv->level[0] == 0 ? 1 : mv->level[0];
   1209 				sc->ariamix_master.treble[1] =
   1210 					mv->level[1] == 0 ? 1 : mv->level[1];
   1211 				error = 0;
   1212 			}
   1213 			break;
   1214 		case ARIAMIX_MASTER_BASS:
   1215 			if (mv->num_channels == 2) {
   1216 				sc->ariamix_master.bass[0] =
   1217 					mv->level[0] == 0 ? 1 : mv->level[0];
   1218 				sc->ariamix_master.bass[1] =
   1219 					mv->level[1] == 0 ? 1 : mv->level[1];
   1220 				error = 0;
   1221 			}
   1222 			break;
   1223 		case ARIAMIX_OUT_LVL:
   1224 			if (mv->num_channels == 1 || mv->num_channels == 2) {
   1225 				sc->sc_gain[0] = mv->level[0];
   1226 				sc->sc_gain[1] = mv->level[1];
   1227 				error = 0;
   1228 			}
   1229 			break;
   1230 		default:
   1231 		}
   1232 	}
   1233 
   1234 	if (cp->type == AUDIO_MIXER_ENUM)
   1235 		switch(cp->dev) {
   1236 		case ARIAMIX_RECORD_SOURCE:
   1237 			if (cp->un.ord>=0 && cp->un.ord<=6) {
   1238 				sc->aria_mix_source = cp->un.ord;
   1239 				error = 0;
   1240 			}
   1241 			break;
   1242 
   1243 		case ARIAMIX_MIC_MUTE:
   1244 			if (cp->un.ord == 0 || cp->un.ord == 1) {
   1245 				sc->aria_mix[ARIAMIX_MIC_LVL].mute =cp->un.ord;
   1246 				error = 0;
   1247 			}
   1248 			break;
   1249 
   1250 		case ARIAMIX_LINE_IN_MUTE:
   1251 			if (cp->un.ord == 0 || cp->un.ord == 1) {
   1252 				sc->aria_mix[ARIAMIX_LINE_IN_LVL].mute =
   1253 					cp->un.ord;
   1254 				error = 0;
   1255 			}
   1256 			break;
   1257 
   1258 		case ARIAMIX_CD_MUTE:
   1259 			if (cp->un.ord == 0 || cp->un.ord == 1) {
   1260 				sc->aria_mix[ARIAMIX_CD_LVL].mute = cp->un.ord;
   1261 				error = 0;
   1262 			}
   1263 			break;
   1264 
   1265 		case ARIAMIX_DAC_MUTE:
   1266 			if (cp->un.ord == 0 || cp->un.ord == 1) {
   1267 				sc->aria_mix[ARIAMIX_DAC_LVL].mute =cp->un.ord;
   1268 				error = 0;
   1269 			}
   1270 			break;
   1271 
   1272 		case ARIAMIX_AUX_MUTE:
   1273 			if (cp->un.ord == 0 || cp->un.ord == 1) {
   1274 				sc->aria_mix[ARIAMIX_AUX_LVL].mute =cp->un.ord;
   1275 				error = 0;
   1276 			}
   1277 			break;
   1278 
   1279 		case ARIAMIX_TEL_MUTE:
   1280 			if (cp->un.ord == 0 || cp->un.ord == 1) {
   1281 				sc->aria_mix[ARIAMIX_TEL_LVL].mute =cp->un.ord;
   1282 				error = 0;
   1283 			}
   1284 			break;
   1285 
   1286 		default:
   1287 			/* NOTREACHED */
   1288 			return ENXIO;
   1289 		}
   1290 
   1291 	return(error);
   1292 }
   1293 
   1294 int
   1295 aria_mixer_get_port(addr, cp)
   1296     void *addr;
   1297     mixer_ctrl_t *cp;
   1298 {
   1299 	struct aria_softc *sc = addr;
   1300 	int error = EINVAL;
   1301 
   1302 	DPRINTF(("aria_mixer_get_port\n"));
   1303 
   1304 	/* This could be done better, no mixer still has some controls. */
   1305 	if (!(ARIA_MIXER&sc->sc_hardware))
   1306 		return ENXIO;
   1307 
   1308 	switch (cp->dev) {
   1309 	case ARIAMIX_MIC_LVL:
   1310 		if (cp->type == AUDIO_MIXER_VALUE) {
   1311 			cp->un.value.num_channels =
   1312 				sc->aria_mix[ARIAMIX_MIC_LVL].num_channels;
   1313 			cp->un.value.level[0] =
   1314 				sc->aria_mix[ARIAMIX_MIC_LVL].level[0];
   1315 			cp->un.value.level[1] =
   1316 				sc->aria_mix[ARIAMIX_MIC_LVL].level[1];
   1317 			error = 0;
   1318 		}
   1319 		break;
   1320 
   1321 	case ARIAMIX_LINE_IN_LVL:
   1322 		if (cp->type == AUDIO_MIXER_VALUE) {
   1323 			cp->un.value.num_channels =
   1324 				sc->aria_mix[ARIAMIX_LINE_IN_LVL].num_channels;
   1325 			cp->un.value.level[0] =
   1326 				sc->aria_mix[ARIAMIX_LINE_IN_LVL].level[0];
   1327 			cp->un.value.level[1] =
   1328 				sc->aria_mix[ARIAMIX_LINE_IN_LVL].level[1];
   1329 			error = 0;
   1330 		}
   1331 		break;
   1332 
   1333 	case ARIAMIX_CD_LVL:
   1334 		if (cp->type == AUDIO_MIXER_VALUE) {
   1335 			cp->un.value.num_channels =
   1336 				sc->aria_mix[ARIAMIX_CD_LVL].num_channels;
   1337 			cp->un.value.level[0] =
   1338 				sc->aria_mix[ARIAMIX_CD_LVL].level[0];
   1339 			cp->un.value.level[1] =
   1340 				sc->aria_mix[ARIAMIX_CD_LVL].level[1];
   1341 			error = 0;
   1342 		}
   1343 		break;
   1344 
   1345 	case ARIAMIX_TEL_LVL:
   1346 		if (cp->type == AUDIO_MIXER_VALUE) {
   1347 			cp->un.value.num_channels =
   1348 				sc->aria_mix[ARIAMIX_TEL_LVL].num_channels;
   1349 			cp->un.value.level[0] =
   1350 				sc->aria_mix[ARIAMIX_TEL_LVL].level[0];
   1351 			error = 0;
   1352 		}
   1353 		break;
   1354 	case ARIAMIX_DAC_LVL:
   1355 		if (cp->type == AUDIO_MIXER_VALUE) {
   1356 			cp->un.value.num_channels =
   1357 				sc->aria_mix[ARIAMIX_DAC_LVL].num_channels;
   1358 			cp->un.value.level[0] =
   1359 				sc->aria_mix[ARIAMIX_DAC_LVL].level[0];
   1360 			cp->un.value.level[1] =
   1361 				sc->aria_mix[ARIAMIX_DAC_LVL].level[1];
   1362 			error = 0;
   1363 		}
   1364 		break;
   1365 
   1366 	case ARIAMIX_AUX_LVL:
   1367 		if (cp->type == AUDIO_MIXER_VALUE) {
   1368 			cp->un.value.num_channels =
   1369 				sc->aria_mix[ARIAMIX_AUX_LVL].num_channels;
   1370 			cp->un.value.level[0] =
   1371 				sc->aria_mix[ARIAMIX_AUX_LVL].level[0];
   1372 			cp->un.value.level[1] =
   1373 				sc->aria_mix[ARIAMIX_AUX_LVL].level[1];
   1374 			error = 0;
   1375 		}
   1376 		break;
   1377 
   1378 	case ARIAMIX_MIC_MUTE:
   1379 		if (cp->type == AUDIO_MIXER_ENUM) {
   1380 			cp->un.ord = sc->aria_mix[ARIAMIX_MIC_LVL].mute;
   1381 			error = 0;
   1382 		}
   1383 		break;
   1384 
   1385 	case ARIAMIX_LINE_IN_MUTE:
   1386 		if (cp->type == AUDIO_MIXER_ENUM) {
   1387 			cp->un.ord = sc->aria_mix[ARIAMIX_LINE_IN_LVL].mute;
   1388 			error = 0;
   1389 		}
   1390 		break;
   1391 
   1392 	case ARIAMIX_CD_MUTE:
   1393 		if (cp->type == AUDIO_MIXER_ENUM) {
   1394 			cp->un.ord = sc->aria_mix[ARIAMIX_CD_LVL].mute;
   1395 			error = 0;
   1396 		}
   1397 		break;
   1398 
   1399 	case ARIAMIX_DAC_MUTE:
   1400 		if (cp->type == AUDIO_MIXER_ENUM) {
   1401 			cp->un.ord = sc->aria_mix[ARIAMIX_DAC_LVL].mute;
   1402 			error = 0;
   1403 		}
   1404 		break;
   1405 
   1406 	case ARIAMIX_AUX_MUTE:
   1407 		if (cp->type == AUDIO_MIXER_ENUM) {
   1408 			cp->un.ord = sc->aria_mix[ARIAMIX_AUX_LVL].mute;
   1409 			error = 0;
   1410 		}
   1411 		break;
   1412 
   1413 	case ARIAMIX_TEL_MUTE:
   1414 		if (cp->type == AUDIO_MIXER_ENUM) {
   1415 			cp->un.ord = sc->aria_mix[ARIAMIX_TEL_LVL].mute;
   1416 			error = 0;
   1417 		}
   1418 		break;
   1419 
   1420 	case ARIAMIX_MASTER_LVL:
   1421 		if (cp->type == AUDIO_MIXER_VALUE) {
   1422 			cp->un.value.num_channels =
   1423 				sc->ariamix_master.num_channels;
   1424 			cp->un.value.level[0] = sc->ariamix_master.level[0];
   1425 			cp->un.value.level[1] = sc->ariamix_master.level[1];
   1426 			error = 0;
   1427 		}
   1428 		break;
   1429 
   1430 	case ARIAMIX_MASTER_TREBLE:
   1431 		if (cp->type == AUDIO_MIXER_VALUE) {
   1432 			cp->un.value.num_channels = 2;
   1433 			cp->un.value.level[0] = sc->ariamix_master.treble[0];
   1434 			cp->un.value.level[1] = sc->ariamix_master.treble[1];
   1435 			error = 0;
   1436 		}
   1437 		break;
   1438 
   1439 	case ARIAMIX_MASTER_BASS:
   1440 		if (cp->type == AUDIO_MIXER_VALUE) {
   1441 			cp->un.value.num_channels = 2;
   1442 			cp->un.value.level[0] = sc->ariamix_master.bass[0];
   1443 			cp->un.value.level[1] = sc->ariamix_master.bass[1];
   1444 			error = 0;
   1445 		}
   1446 		break;
   1447 
   1448 	case ARIAMIX_OUT_LVL:
   1449 		if (cp->type == AUDIO_MIXER_VALUE) {
   1450 			cp->un.value.num_channels = sc->sc_chans;
   1451 			cp->un.value.level[0] = sc->sc_gain[0];
   1452 			cp->un.value.level[1] = sc->sc_gain[1];
   1453 			error = 0;
   1454 		}
   1455 		break;
   1456 	case ARIAMIX_RECORD_SOURCE:
   1457 		if (cp->type == AUDIO_MIXER_ENUM) {
   1458 			cp->un.ord = sc->aria_mix_source;
   1459 			error = 0;
   1460 		}
   1461 		break;
   1462 
   1463 	default:
   1464 		return ENXIO;
   1465 		/* NOT REACHED */
   1466 	}
   1467 
   1468 	return(error);
   1469 }
   1470 
   1471 int
   1472 aria_mixer_query_devinfo(addr, dip)
   1473 	   void *addr;
   1474 	   mixer_devinfo_t *dip;
   1475 {
   1476 
   1477 	struct aria_softc *sc = addr;
   1478 
   1479 	DPRINTF(("aria_mixer_query_devinfo\n"));
   1480 
   1481 	/* This could be done better, no mixer still has some controls. */
   1482 	if (!(ARIA_MIXER & sc->sc_hardware))
   1483 		return ENXIO;
   1484 
   1485 	dip->prev = dip->next = AUDIO_MIXER_LAST;
   1486 
   1487 	switch(dip->index) {
   1488 	case ARIAMIX_MIC_LVL:
   1489 		dip->type = AUDIO_MIXER_VALUE;
   1490 		dip->mixer_class = ARIAMIX_INPUT_CLASS;
   1491 		dip->next = ARIAMIX_MIC_MUTE;
   1492 		strcpy(dip->label.name, AudioNmicrophone);
   1493 		dip->un.v.num_channels = 2;
   1494 		strcpy(dip->un.v.units.name, AudioNvolume);
   1495 		break;
   1496 
   1497 	case ARIAMIX_LINE_IN_LVL:
   1498 		dip->type = AUDIO_MIXER_VALUE;
   1499 		dip->mixer_class = ARIAMIX_INPUT_CLASS;
   1500 		dip->next = ARIAMIX_LINE_IN_MUTE;
   1501 		strcpy(dip->label.name, AudioNline);
   1502 		dip->un.v.num_channels = 2;
   1503 		strcpy(dip->un.v.units.name, AudioNvolume);
   1504 		break;
   1505 
   1506 	case ARIAMIX_CD_LVL:
   1507 		dip->type = AUDIO_MIXER_VALUE;
   1508 		dip->mixer_class = ARIAMIX_INPUT_CLASS;
   1509 		dip->next = ARIAMIX_CD_MUTE;
   1510 		strcpy(dip->label.name, AudioNcd);
   1511 		dip->un.v.num_channels = 2;
   1512 		strcpy(dip->un.v.units.name, AudioNvolume);
   1513 		break;
   1514 
   1515 	case ARIAMIX_TEL_LVL:
   1516 		dip->type = AUDIO_MIXER_VALUE;
   1517 		dip->mixer_class = ARIAMIX_INPUT_CLASS;
   1518 		dip->next = ARIAMIX_TEL_MUTE;
   1519 		strcpy(dip->label.name, "telephone");
   1520 		dip->un.v.num_channels = 1;
   1521 		strcpy(dip->un.v.units.name, AudioNvolume);
   1522 		break;
   1523 
   1524 	case ARIAMIX_DAC_LVL:
   1525 		dip->type = AUDIO_MIXER_VALUE;
   1526 		dip->mixer_class = ARIAMIX_INPUT_CLASS;
   1527 		dip->next = ARIAMIX_DAC_MUTE;
   1528 		strcpy(dip->label.name, AudioNdac);
   1529 		dip->un.v.num_channels = 1;
   1530 		strcpy(dip->un.v.units.name, AudioNvolume);
   1531 		break;
   1532 
   1533 	case ARIAMIX_AUX_LVL:
   1534 		dip->type = AUDIO_MIXER_VALUE;
   1535 		dip->mixer_class = ARIAMIX_INPUT_CLASS;
   1536 		dip->next = ARIAMIX_AUX_MUTE;
   1537 		strcpy(dip->label.name, AudioNoutput);
   1538 		dip->un.v.num_channels = 1;
   1539 		strcpy(dip->un.v.units.name, AudioNvolume);
   1540 		break;
   1541 
   1542 	case ARIAMIX_MIC_MUTE:
   1543 		dip->prev = ARIAMIX_MIC_LVL;
   1544 		goto mute;
   1545 
   1546 	case ARIAMIX_LINE_IN_MUTE:
   1547 		dip->prev = ARIAMIX_LINE_IN_LVL;
   1548 		goto mute;
   1549 
   1550 	case ARIAMIX_CD_MUTE:
   1551 		dip->prev = ARIAMIX_CD_LVL;
   1552 		goto mute;
   1553 
   1554 	case ARIAMIX_DAC_MUTE:
   1555 		dip->prev = ARIAMIX_DAC_LVL;
   1556 		goto mute;
   1557 
   1558 	case ARIAMIX_AUX_MUTE:
   1559 		dip->prev = ARIAMIX_AUX_LVL;
   1560 		goto mute;
   1561 
   1562 	case ARIAMIX_TEL_MUTE:
   1563 		dip->prev = ARIAMIX_TEL_LVL;
   1564 		goto mute;
   1565 
   1566 mute:
   1567 		dip->mixer_class = ARIAMIX_INPUT_CLASS;
   1568 		dip->type = AUDIO_MIXER_ENUM;
   1569 		strcpy(dip->label.name, AudioNmute);
   1570 		dip->un.e.num_mem = 2;
   1571 		strcpy(dip->un.e.member[0].label.name, AudioNoff);
   1572 		dip->un.e.member[0].ord = 0;
   1573 		strcpy(dip->un.e.member[1].label.name, AudioNon);
   1574 		dip->un.e.member[1].ord = 1;
   1575 		break;
   1576 
   1577 	case ARIAMIX_MASTER_LVL:
   1578 		dip->type = AUDIO_MIXER_VALUE;
   1579 		dip->mixer_class = ARIAMIX_OUTPUT_CLASS;
   1580 		dip->next = AUDIO_MIXER_LAST;
   1581 		strcpy(dip->label.name, AudioNvolume);
   1582 		dip->un.v.num_channels = 2;
   1583 		strcpy(dip->un.v.units.name, AudioNvolume);
   1584 		break;
   1585 
   1586 	case ARIAMIX_MASTER_TREBLE:
   1587 		dip->type = AUDIO_MIXER_VALUE;
   1588 		dip->mixer_class = ARIAMIX_EQ_CLASS;
   1589 		strcpy(dip->label.name, AudioNtreble);
   1590 		dip->un.v.num_channels = 2;
   1591 		strcpy(dip->un.v.units.name, AudioNtreble);
   1592 		break;
   1593 
   1594 	case ARIAMIX_MASTER_BASS:
   1595 		dip->type = AUDIO_MIXER_VALUE;
   1596 		dip->mixer_class = ARIAMIX_EQ_CLASS;
   1597 		strcpy(dip->label.name, AudioNbass);
   1598 		dip->un.v.num_channels = 2;
   1599 		strcpy(dip->un.v.units.name, AudioNbass);
   1600 		break;
   1601 
   1602 	case ARIAMIX_OUT_LVL:
   1603 		dip->type = AUDIO_MIXER_VALUE;
   1604 		dip->mixer_class = ARIAMIX_OUTPUT_CLASS;
   1605 		strcpy(dip->label.name, AudioNoutput);
   1606 		dip->un.v.num_channels = 2;
   1607 		strcpy(dip->un.v.units.name, AudioNvolume);
   1608 		break;
   1609 
   1610 	case ARIAMIX_RECORD_SOURCE:
   1611 		dip->mixer_class = ARIAMIX_RECORD_CLASS;
   1612 		dip->type = AUDIO_MIXER_ENUM;
   1613 		strcpy(dip->label.name, AudioNsource);
   1614 		dip->un.e.num_mem = 6;
   1615 		strcpy(dip->un.e.member[0].label.name, AudioNoutput);
   1616 		dip->un.e.member[0].ord = ARIAMIX_AUX_LVL;
   1617 		strcpy(dip->un.e.member[1].label.name, AudioNmicrophone);
   1618 		dip->un.e.member[1].ord = ARIAMIX_MIC_LVL;
   1619 		strcpy(dip->un.e.member[2].label.name, AudioNdac);
   1620 		dip->un.e.member[2].ord = ARIAMIX_DAC_LVL;
   1621 		strcpy(dip->un.e.member[3].label.name, AudioNline);
   1622 		dip->un.e.member[3].ord = ARIAMIX_LINE_IN_LVL;
   1623 		strcpy(dip->un.e.member[4].label.name, AudioNcd);
   1624 		dip->un.e.member[4].ord = ARIAMIX_CD_LVL;
   1625 		strcpy(dip->un.e.member[5].label.name, "telephone");
   1626 		dip->un.e.member[5].ord = ARIAMIX_TEL_LVL;
   1627 		break;
   1628 
   1629 	case ARIAMIX_INPUT_CLASS:
   1630 		dip->type = AUDIO_MIXER_CLASS;
   1631 		dip->mixer_class = ARIAMIX_INPUT_CLASS;
   1632 		strcpy(dip->label.name, AudioCinputs);
   1633 		break;
   1634 
   1635 	case ARIAMIX_OUTPUT_CLASS:
   1636 		dip->type = AUDIO_MIXER_CLASS;
   1637 		dip->mixer_class = ARIAMIX_OUTPUT_CLASS;
   1638 		strcpy(dip->label.name, AudioCoutputs);
   1639 		break;
   1640 
   1641 	case ARIAMIX_RECORD_CLASS:
   1642 		dip->type = AUDIO_MIXER_CLASS;
   1643 		dip->mixer_class = ARIAMIX_RECORD_CLASS;
   1644 		strcpy(dip->label.name, AudioCrecord);
   1645 		break;
   1646 
   1647 	case ARIAMIX_EQ_CLASS:
   1648 		dip->type = AUDIO_MIXER_CLASS;
   1649 		dip->mixer_class = ARIAMIX_EQ_CLASS;
   1650 		strcpy(dip->label.name, AudioCequalization);
   1651 		break;
   1652 
   1653 	default:
   1654 		return ENXIO;
   1655 		/*NOTREACHED*/
   1656 	}
   1657 	return 0;
   1658 }
   1659