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