Home | History | Annotate | Line # | Download | only in isa
aria.c revision 1.25
      1 /*	$NetBSD: aria.c,v 1.25 2006/08/27 23:33:35 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.25 2006/08/27 23:33:35 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, const audio_params_t *param)
    580 {
    581 	int i;
    582 
    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(void *addr)
    595 {
    596 
    597 	return AUDIO_PROP_FULLDUPLEX;
    598 }
    599 
    600 int
    601 aria_set_params(
    602 	void *addr,
    603 	int setmode, int usemode,
    604 	audio_params_t *p, audio_params_t *r,
    605 	stream_filter_list_t *pfil, stream_filter_list_t *rfil)
    606 {
    607 	audio_params_t hw;
    608 	struct aria_softc *sc;
    609 
    610 	sc = addr;
    611 	switch(p->encoding) {
    612 	case AUDIO_ENCODING_ULAW:
    613 	case AUDIO_ENCODING_ALAW:
    614 	case AUDIO_ENCODING_SLINEAR:
    615 	case AUDIO_ENCODING_SLINEAR_LE:
    616 	case AUDIO_ENCODING_SLINEAR_BE:
    617 	case AUDIO_ENCODING_ULINEAR:
    618 	case AUDIO_ENCODING_ULINEAR_LE:
    619 	case AUDIO_ENCODING_ULINEAR_BE:
    620 		break;
    621 	default:
    622 		return EINVAL;
    623 	}
    624 
    625 	if (p->sample_rate <= 9450)
    626 		p->sample_rate = 7875;
    627 	else if (p->sample_rate <= 13387)
    628 		p->sample_rate = 11025;
    629 	else if (p->sample_rate <= 18900)
    630 		p->sample_rate = 15750;
    631 	else if (p->sample_rate <= 26775)
    632 		p->sample_rate = 22050;
    633 	else if (p->sample_rate <= 37800)
    634 		p->sample_rate = 31500;
    635 	else
    636 		p->sample_rate = 44100;
    637 
    638 	hw = *p;
    639 	sc->sc_encoding = p->encoding;
    640 	sc->sc_precision = p->precision;
    641 	sc->sc_chans = p->channels;
    642 	sc->sc_rate = p->sample_rate;
    643 
    644 	switch(p->encoding) {
    645 	case AUDIO_ENCODING_ULAW:
    646 		if ((ARIA_MODEL&sc->sc_hardware) == 0) {
    647 			hw.encoding = AUDIO_ENCODING_ULINEAR_LE;
    648 			pfil->append(pfil, mulaw_to_linear8, &hw);
    649 			rfil->append(rfil, linear8_to_mulaw, &hw);
    650 		}
    651 		break;
    652 	case AUDIO_ENCODING_ALAW:
    653 		if ((ARIA_MODEL&sc->sc_hardware) == 0) {
    654 			hw.encoding = AUDIO_ENCODING_ULINEAR_LE;
    655 			pfil->append(pfil, alaw_to_linear8, &hw);
    656 			rfil->append(rfil, linear8_to_alaw, &hw);
    657 		}
    658 		break;
    659 	case AUDIO_ENCODING_SLINEAR:
    660 		hw.encoding = AUDIO_ENCODING_ULINEAR_LE;
    661 		pfil->append(pfil, change_sign8, &hw);
    662 		rfil->append(rfil, change_sign8, &hw);
    663 		break;
    664 	case AUDIO_ENCODING_ULINEAR_LE:
    665 		hw.encoding = AUDIO_ENCODING_SLINEAR_LE;
    666 		pfil->append(pfil, change_sign16, &hw);
    667 		rfil->append(rfil, change_sign16, &hw);
    668 		break;
    669 	case AUDIO_ENCODING_SLINEAR_BE:
    670 		hw.encoding = AUDIO_ENCODING_SLINEAR_LE;
    671 		pfil->append(pfil, swap_bytes, &hw);
    672 		rfil->append(rfil, swap_bytes, &hw);
    673 		break;
    674 	case AUDIO_ENCODING_ULINEAR_BE:
    675 		hw.encoding = AUDIO_ENCODING_SLINEAR_LE;
    676 		pfil->append(pfil, swap_bytes_change_sign16, &hw);
    677 		rfil->append(rfil, swap_bytes_change_sign16, &hw);
    678 		break;
    679 	}
    680 
    681 	return 0;
    682 }
    683 
    684 /*
    685  * This is where all of the twiddling goes on.
    686  */
    687 
    688 int
    689 aria_commit_settings(void *addr)
    690 {
    691 	static u_char tones[16] =
    692 	    { 7, 6, 5, 4, 3, 2, 1, 0, 8, 9, 10, 11, 12, 13, 14, 15 };
    693 	struct aria_softc *sc;
    694 	bus_space_tag_t iot;
    695 	bus_space_handle_t ioh;
    696 	u_short format;
    697 	u_short left, right;
    698 	u_short samp;
    699 	u_char i;
    700 
    701 	DPRINTF(("aria_commit_settings\n"));
    702 
    703 	sc = addr;
    704 	iot = sc->sc_iot;
    705 	ioh = sc->sc_ioh;
    706 	switch (sc->sc_rate) {
    707 	case  7875: format = 0x00; samp = 0x60; break;
    708 	case 11025: format = 0x00; samp = 0x40; break;
    709 	case 15750: format = 0x10; samp = 0x60; break;
    710 	case 22050: format = 0x10; samp = 0x40; break;
    711 	case 31500: format = 0x10; samp = 0x20; break;
    712 	case 44100: format = 0x20; samp = 0x00; break;
    713 	default:    format = 0x00; samp = 0x40; break;/* XXX can we get here? */
    714 	}
    715 
    716 	if ((ARIA_MODEL&sc->sc_hardware) != 0) {
    717 		format |= sc->sc_encoding == AUDIO_ENCODING_ULAW ? 0x06 : 0x00;
    718 		format |= sc->sc_encoding == AUDIO_ENCODING_ALAW ? 0x08 : 0x00;
    719 	}
    720 
    721 	format |= (sc->sc_precision == 16) ? 0x02 : 0x00;
    722 	format |= (sc->sc_chans == 2) ? 1 : 0;
    723 	samp |= bus_space_read_2(iot, ioh, ARIADSP_STATUS) & ~0x60;
    724 
    725 	aria_sendcmd(sc, ARIADSPC_FORMAT, format, -1, -1);
    726 	bus_space_write_2(iot, ioh, ARIADSP_CONTROL, samp);
    727 
    728 	if (sc->sc_hardware&ARIA_MIXER) {
    729 		for (i = 0; i < 6; i++)
    730 			aria_set_mixer(sc, i);
    731 
    732 		if (sc->sc_chans==2) {
    733 			aria_sendcmd(sc, ARIADSPC_CHAN_VOL, ARIAR_PLAY_CHAN,
    734 				     ((sc->sc_gain[0]+sc->sc_gain[1])/2)<<7,
    735 				     -1);
    736 			aria_sendcmd(sc, ARIADSPC_CHAN_PAN, ARIAR_PLAY_CHAN,
    737 				     (sc->sc_gain[0]-sc->sc_gain[1])/4+0x40,
    738 				     -1);
    739 		} else {
    740 			aria_sendcmd(sc, ARIADSPC_CHAN_VOL, ARIAR_PLAY_CHAN,
    741 				     sc->sc_gain[0]<<7, -1);
    742 			aria_sendcmd(sc, ARIADSPC_CHAN_PAN, ARIAR_PLAY_CHAN,
    743 				     0x40, -1);
    744 		}
    745 
    746 		aria_sendcmd(sc, ARIADSPC_MASMONMODE,
    747 			     sc->ariamix_master.num_channels != 2, -1, -1);
    748 
    749 		aria_sendcmd(sc, ARIADSPC_MIXERVOL, 0x0004,
    750 			     sc->ariamix_master.level[0] << 7,
    751 			     sc->ariamix_master.level[1] << 7);
    752 
    753 		/* Convert treble/bass from byte to soundcard style */
    754 
    755 		left  = (tones[(sc->ariamix_master.treble[0]>>4)&0x0f]<<8) |
    756 			 tones[(sc->ariamix_master.bass[0]>>4)&0x0f];
    757 		right = (tones[(sc->ariamix_master.treble[1]>>4)&0x0f]<<8) |
    758 			 tones[(sc->ariamix_master.bass[1]>>4)&0x0f];
    759 
    760 		aria_sendcmd(sc, ARIADSPC_TONE, left, right, -1);
    761 	}
    762 
    763 	aria_sendcmd(sc, ARIADSPC_BLOCKSIZE, sc->sc_blocksize/2, -1, -1);
    764 
    765 /*
    766  * If we think that the card is recording or playing, start it up again here.
    767  * Some of the previous commands turn the channels off.
    768  */
    769 
    770 	if (sc->sc_record&(1<<ARIAR_RECORD_CHAN))
    771 		aria_sendcmd(sc, ARIADSPC_START_REC, ARIAR_RECORD_CHAN, -1,-1);
    772 
    773 	if (sc->sc_play&(1<<ARIAR_PLAY_CHAN))
    774 		aria_sendcmd(sc, ARIADSPC_START_PLAY, ARIAR_PLAY_CHAN, -1, -1);
    775 
    776 	return 0;
    777 }
    778 
    779 void
    780 aria_set_mixer(struct aria_softc *sc, int i)
    781 {
    782 	u_char source;
    783 
    784 	switch(i) {
    785 	case ARIAMIX_MIC_LVL:     source = 0x0001; break;
    786 	case ARIAMIX_CD_LVL:      source = 0x0002; break;
    787 	case ARIAMIX_LINE_IN_LVL: source = 0x0008; break;
    788 	case ARIAMIX_TEL_LVL:     source = 0x0020; break;
    789 	case ARIAMIX_AUX_LVL:     source = 0x0010; break;
    790 	case ARIAMIX_DAC_LVL:     source = 0x0004; break;
    791 	default:		  source = 0x0000; break;
    792 	}
    793 
    794 	if (source != 0x0000 && source != 0x0004) {
    795 		if (sc->aria_mix[i].mute == 1)
    796 			aria_sendcmd(sc, ARIADSPC_INPMONMODE, source, 3, -1);
    797 		else
    798 			aria_sendcmd(sc, ARIADSPC_INPMONMODE, source,
    799 				     sc->aria_mix[i].num_channels != 2, -1);
    800 
    801 		aria_sendcmd(sc, ARIADSPC_INPMONMODE, 0x8000|source,
    802 			     sc->aria_mix[i].num_channels != 2, -1);
    803 		aria_sendcmd(sc, ARIADSPC_MIXERVOL, source,
    804 			     sc->aria_mix[i].level[0] << 7,
    805 			     sc->aria_mix[i].level[1] << 7);
    806 	}
    807 
    808 	if (sc->aria_mix_source == i) {
    809 		aria_sendcmd(sc, ARIADSPC_ADCSOURCE, source, -1, -1);
    810 
    811 		if (sc->sc_open & ARIAR_OPEN_RECORD)
    812 			aria_sendcmd(sc, ARIADSPC_ADCCONTROL, 1, -1, -1);
    813 		else
    814 			aria_sendcmd(sc, ARIADSPC_ADCCONTROL, 0, -1, -1);
    815 	}
    816 }
    817 
    818 void
    819 ariaclose(void *addr)
    820 {
    821 	struct aria_softc *sc;
    822 
    823 	sc = addr;
    824 	DPRINTF(("aria_close sc=%p\n", sc));
    825 
    826 	sc->sc_open = 0;
    827 
    828 	if (aria_reset(sc) != 0) {
    829 		delay(500);
    830 		aria_reset(sc);
    831 	}
    832 }
    833 
    834 /*
    835  * Reset the hardware.
    836  */
    837 
    838 int ariareset(bus_space_tag_t iot, bus_space_handle_t ioh)
    839 {
    840 	struct aria_softc tmp, *sc;
    841 
    842 	sc = &tmp;
    843 	sc->sc_iot = iot;
    844 	sc->sc_ioh = ioh;
    845 	return aria_reset(sc);
    846 }
    847 
    848 int
    849 aria_reset(struct aria_softc *sc)
    850 {
    851 	bus_space_tag_t iot;
    852 	bus_space_handle_t ioh;
    853 	int fail;
    854 	int i;
    855 
    856 	iot = sc->sc_iot;
    857 	ioh = sc->sc_ioh;
    858 	fail = 0;
    859 	bus_space_write_2(iot, ioh, ARIADSP_CONTROL,
    860 			  ARIAR_ARIA_SYNTH | ARIAR_SR22K|ARIAR_DSPINTWR);
    861 	aria_putdspmem(sc, 0x6102, 0);
    862 
    863 	fail |= aria_sendcmd(sc, ARIADSPC_SYSINIT, 0x0000, 0x0000, 0x0000);
    864 
    865 	for (i=0; i < ARIAR_NPOLL; i++)
    866 		if (aria_getdspmem(sc, ARIAA_TASK_A) == 1)
    867 			break;
    868 
    869 	bus_space_write_2(iot, ioh, ARIADSP_CONTROL,
    870 			  ARIAR_ARIA_SYNTH|ARIAR_SR22K | ARIAR_DSPINTWR |
    871 			  ARIAR_PCINTWR);
    872 	fail |= aria_sendcmd(sc, ARIADSPC_MODE, ARIAV_MODE_NO_SYNTH,-1,-1);
    873 
    874 	return fail;
    875 }
    876 
    877 /*
    878  * Lower-level routines
    879  */
    880 
    881 void
    882 aria_putdspmem(struct aria_softc *sc, u_short loc, u_short val)
    883 {
    884 	bus_space_tag_t iot;
    885 	bus_space_handle_t ioh;
    886 
    887 	iot = sc->sc_iot;
    888 	ioh = sc->sc_ioh;
    889 	bus_space_write_2(iot, ioh, ARIADSP_DMAADDRESS, loc);
    890 	bus_space_write_2(iot, ioh, ARIADSP_DMADATA, val);
    891 }
    892 
    893 u_short
    894 aria_getdspmem(struct aria_softc *sc, u_short loc)
    895 {
    896 	bus_space_tag_t iot;
    897 	bus_space_handle_t ioh;
    898 
    899 	iot = sc->sc_iot;
    900 	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(struct aria_softc *sc, u_short command,
    922 	     int arg1, int arg2, int arg3)
    923 {
    924 	bus_space_tag_t iot;
    925 	bus_space_handle_t ioh;
    926 	int i, fail;
    927 
    928 	iot = sc->sc_iot;
    929 	ioh = sc->sc_ioh;
    930 	fail = 0;
    931 	ARIASEND(command, 1);
    932 	if (arg1 != -1) {
    933 		ARIASEND(arg1, 2);
    934 	}
    935 	if (arg2 != -1) {
    936 		ARIASEND(arg2, 4);
    937 	}
    938 	if (arg3 != -1) {
    939 		ARIASEND(arg3, 8);
    940 	}
    941 	ARIASEND(ARIADSPC_TERM, 16);
    942 
    943 	if (fail) {
    944 		sc->sc_sendcmd_err++;
    945 #ifdef AUDIO_DEBUG
    946 		DPRINTF(("aria_sendcmd: failure=(%d) cmd=(0x%x) fail=(0x%x)\n",
    947 			 sc->sc_sendcmd_err, command, fail));
    948 #endif
    949 		return -1;
    950 	}
    951 
    952 	return 0;
    953 }
    954 #undef ARIASEND
    955 
    956 int
    957 aria_halt_input(void *addr)
    958 {
    959 	struct aria_softc *sc;
    960 
    961 	sc = addr;
    962 	DPRINTF(("aria_halt_input\n"));
    963 
    964 	if (sc->sc_record & (1<<0)) {
    965 		aria_sendcmd(sc, ARIADSPC_STOP_REC, 0, -1, -1);
    966 		sc->sc_record &= ~(1<<0);
    967 		sc->sc_rdiobuffer = 0;
    968 	}
    969 
    970 	return 0;
    971 }
    972 
    973 int
    974 aria_halt_output(void *addr)
    975 {
    976 	struct aria_softc *sc;
    977 
    978 	sc = addr;
    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 		sc->sc_pdiobuffer = 0;
    985 	}
    986 
    987 	return 0;
    988 }
    989 
    990 /*
    991  * Here we just set up the buffers.  If we receive
    992  * an interrupt without these set, it is ignored.
    993  */
    994 
    995 int
    996 aria_start_input(void *addr, void *p, int cc, void (*intr)(void *), void *arg)
    997 {
    998 	struct aria_softc *sc;
    999 
   1000 	sc = addr;
   1001 	DPRINTF(("aria_start_input %d @ %p\n", cc, p));
   1002 
   1003 	if (cc != sc->sc_blocksize) {
   1004 		DPRINTF(("aria_start_input reqsize %d not sc_blocksize %d\n",
   1005 			cc, sc->sc_blocksize));
   1006 		return EINVAL;
   1007 	}
   1008 
   1009 	sc->sc_rarg = arg;
   1010 	sc->sc_rintr = intr;
   1011 	sc->sc_rdiobuffer = p;
   1012 
   1013 	if (!(sc->sc_record&(1<<ARIAR_RECORD_CHAN))) {
   1014 		aria_sendcmd(sc, ARIADSPC_START_REC, ARIAR_RECORD_CHAN, -1,-1);
   1015 		sc->sc_record |= (1<<ARIAR_RECORD_CHAN);
   1016 	}
   1017 
   1018 	return 0;
   1019 }
   1020 
   1021 int
   1022 aria_start_output(void *addr, void *p, int cc, void (*intr)(void *), void *arg)
   1023 {
   1024 	struct aria_softc *sc;
   1025 
   1026 	sc = addr;
   1027 	DPRINTF(("aria_start_output %d @ %p\n", cc, p));
   1028 
   1029 	if (cc != sc->sc_blocksize) {
   1030 		DPRINTF(("aria_start_output reqsize %d not sc_blocksize %d\n",
   1031 			cc, sc->sc_blocksize));
   1032 		return EINVAL;
   1033 	}
   1034 
   1035 	sc->sc_parg = arg;
   1036 	sc->sc_pintr = intr;
   1037 	sc->sc_pdiobuffer = p;
   1038 
   1039 	if (!(sc->sc_play&(1<<ARIAR_PLAY_CHAN))) {
   1040 		aria_sendcmd(sc, ARIADSPC_START_PLAY, ARIAR_PLAY_CHAN, -1, -1);
   1041 		sc->sc_play |= (1<<ARIAR_PLAY_CHAN);
   1042 	}
   1043 
   1044 	return 0;
   1045 }
   1046 
   1047 /*
   1048  * Process an interrupt.  This should be a
   1049  * request (from the card) to write or read
   1050  * samples.
   1051  */
   1052 int
   1053 aria_intr(void *arg)
   1054 {
   1055 	struct aria_softc *sc;
   1056 	bus_space_tag_t iot;
   1057 	bus_space_handle_t ioh;
   1058 	u_short *pdata;
   1059 	u_short *rdata;
   1060 	u_short address;
   1061 
   1062 	sc = arg;
   1063 	iot = sc->sc_iot;
   1064 	ioh = sc->sc_ioh;
   1065 	pdata = sc->sc_pdiobuffer;
   1066 	rdata = sc->sc_rdiobuffer;
   1067 #if 0 /*  XXX --  BAD BAD BAD (That this is #define'd out */
   1068 	DPRINTF(("Checking to see if this is our intr\n"));
   1069 
   1070 	if ((inw(iobase) & 1) != 0x1)
   1071 		return 0;  /* not for us */
   1072 #endif
   1073 
   1074 	sc->sc_interrupts++;
   1075 
   1076 	DPRINTF(("aria_intr\n"));
   1077 
   1078 	if ((sc->sc_open & ARIAR_OPEN_PLAY) && (pdata!=NULL)) {
   1079 		DPRINTF(("aria_intr play=(%x)\n", (unsigned) pdata));
   1080 		address = 0x8000 - 2*(sc->sc_blocksize);
   1081 		address+= aria_getdspmem(sc, ARIAA_PLAY_FIFO_A);
   1082 		bus_space_write_2(iot, ioh, ARIADSP_DMAADDRESS, address);
   1083 		bus_space_write_multi_2(iot, ioh, ARIADSP_DMADATA, pdata,
   1084 					sc->sc_blocksize / 2);
   1085 		if (sc->sc_pintr != NULL)
   1086 			(*sc->sc_pintr)(sc->sc_parg);
   1087 	}
   1088 
   1089 	if ((sc->sc_open & ARIAR_OPEN_RECORD) && (rdata!=NULL)) {
   1090 		DPRINTF(("aria_intr record=(%x)\n", (unsigned) rdata));
   1091 		address = 0x8000 - (sc->sc_blocksize);
   1092 		address+= aria_getdspmem(sc, ARIAA_REC_FIFO_A);
   1093 		bus_space_write_2(iot, ioh, ARIADSP_DMAADDRESS, address);
   1094 		bus_space_read_multi_2(iot, ioh, ARIADSP_DMADATA, rdata,
   1095 				       sc->sc_blocksize / 2);
   1096 		if (sc->sc_rintr != NULL)
   1097 			(*sc->sc_rintr)(sc->sc_rarg);
   1098 	}
   1099 
   1100 	aria_sendcmd(sc, ARIADSPC_TRANSCOMPLETE, -1, -1, -1);
   1101 
   1102 	return 1;
   1103 }
   1104 
   1105 int
   1106 aria_mixer_set_port(void *addr, mixer_ctrl_t *cp)
   1107 {
   1108 	struct aria_softc *sc;
   1109 	int error;
   1110 
   1111 	DPRINTF(("aria_mixer_set_port\n"));
   1112 	sc = addr;
   1113 	error = EINVAL;
   1114 
   1115 	/* This could be done better, no mixer still has some controls. */
   1116 	if (!(ARIA_MIXER & sc->sc_hardware))
   1117 		return ENXIO;
   1118 
   1119 	if (cp->type == AUDIO_MIXER_VALUE) {
   1120 		mixer_level_t *mv = &cp->un.value;
   1121 		switch (cp->dev) {
   1122 		case ARIAMIX_MIC_LVL:
   1123 			if (mv->num_channels == 1 || mv->num_channels == 2) {
   1124 				sc->aria_mix[ARIAMIX_MIC_LVL].num_channels =
   1125 					mv->num_channels;
   1126 				sc->aria_mix[ARIAMIX_MIC_LVL].level[0] =
   1127 					mv->level[0];
   1128 				sc->aria_mix[ARIAMIX_MIC_LVL].level[1] =
   1129 					mv->level[1];
   1130 				error = 0;
   1131 			}
   1132 			break;
   1133 
   1134 		case ARIAMIX_LINE_IN_LVL:
   1135 			if (mv->num_channels == 1 || mv->num_channels == 2) {
   1136 				sc->aria_mix[ARIAMIX_LINE_IN_LVL].num_channels=
   1137 					mv->num_channels;
   1138 				sc->aria_mix[ARIAMIX_LINE_IN_LVL].level[0] =
   1139 					mv->level[0];
   1140 				sc->aria_mix[ARIAMIX_LINE_IN_LVL].level[1] =
   1141 					mv->level[1];
   1142 				error = 0;
   1143 			}
   1144 			break;
   1145 
   1146 		case ARIAMIX_CD_LVL:
   1147 			if (mv->num_channels == 1 || mv->num_channels == 2) {
   1148 				sc->aria_mix[ARIAMIX_CD_LVL].num_channels =
   1149 					mv->num_channels;
   1150 				sc->aria_mix[ARIAMIX_CD_LVL].level[0] =
   1151 					mv->level[0];
   1152 				sc->aria_mix[ARIAMIX_CD_LVL].level[1] =
   1153 					mv->level[1];
   1154 				error = 0;
   1155 			}
   1156 			break;
   1157 
   1158 		case ARIAMIX_TEL_LVL:
   1159 			if (mv->num_channels == 1) {
   1160 				sc->aria_mix[ARIAMIX_TEL_LVL].num_channels =
   1161 					mv->num_channels;
   1162 				sc->aria_mix[ARIAMIX_TEL_LVL].level[0] =
   1163 					mv->level[0];
   1164 				error = 0;
   1165 			}
   1166 			break;
   1167 
   1168 		case ARIAMIX_DAC_LVL:
   1169 			if (mv->num_channels == 1 || mv->num_channels == 2) {
   1170 				sc->aria_mix[ARIAMIX_DAC_LVL].num_channels =
   1171 					mv->num_channels;
   1172 				sc->aria_mix[ARIAMIX_DAC_LVL].level[0] =
   1173 					mv->level[0];
   1174 				sc->aria_mix[ARIAMIX_DAC_LVL].level[1] =
   1175 					mv->level[1];
   1176 				error = 0;
   1177 			}
   1178 			break;
   1179 
   1180 		case ARIAMIX_AUX_LVL:
   1181 			if (mv->num_channels == 1 || mv->num_channels == 2) {
   1182 				sc->aria_mix[ARIAMIX_AUX_LVL].num_channels =
   1183 					mv->num_channels;
   1184 				sc->aria_mix[ARIAMIX_AUX_LVL].level[0] =
   1185 					mv->level[0];
   1186 				sc->aria_mix[ARIAMIX_AUX_LVL].level[1] =
   1187 					mv->level[1];
   1188 				error = 0;
   1189 			}
   1190 			break;
   1191 
   1192 		case ARIAMIX_MASTER_LVL:
   1193 			if (mv->num_channels == 1 || mv->num_channels == 2) {
   1194 				sc->ariamix_master.num_channels =
   1195 					mv->num_channels;
   1196 				sc->ariamix_master.level[0] = mv->level[0];
   1197 				sc->ariamix_master.level[1] = mv->level[1];
   1198 				error = 0;
   1199 			}
   1200 			break;
   1201 
   1202 		case ARIAMIX_MASTER_TREBLE:
   1203 			if (mv->num_channels == 2) {
   1204 				sc->ariamix_master.treble[0] =
   1205 					mv->level[0] == 0 ? 1 : mv->level[0];
   1206 				sc->ariamix_master.treble[1] =
   1207 					mv->level[1] == 0 ? 1 : mv->level[1];
   1208 				error = 0;
   1209 			}
   1210 			break;
   1211 		case ARIAMIX_MASTER_BASS:
   1212 			if (mv->num_channels == 2) {
   1213 				sc->ariamix_master.bass[0] =
   1214 					mv->level[0] == 0 ? 1 : mv->level[0];
   1215 				sc->ariamix_master.bass[1] =
   1216 					mv->level[1] == 0 ? 1 : mv->level[1];
   1217 				error = 0;
   1218 			}
   1219 			break;
   1220 		case ARIAMIX_OUT_LVL:
   1221 			if (mv->num_channels == 1 || mv->num_channels == 2) {
   1222 				sc->sc_gain[0] = mv->level[0];
   1223 				sc->sc_gain[1] = mv->level[1];
   1224 				error = 0;
   1225 			}
   1226 			break;
   1227 		default:
   1228 			break;
   1229 		}
   1230 	}
   1231 
   1232 	if (cp->type == AUDIO_MIXER_ENUM)
   1233 		switch(cp->dev) {
   1234 		case ARIAMIX_RECORD_SOURCE:
   1235 			if (cp->un.ord>=0 && cp->un.ord<=6) {
   1236 				sc->aria_mix_source = cp->un.ord;
   1237 				error = 0;
   1238 			}
   1239 			break;
   1240 
   1241 		case ARIAMIX_MIC_MUTE:
   1242 			if (cp->un.ord == 0 || cp->un.ord == 1) {
   1243 				sc->aria_mix[ARIAMIX_MIC_LVL].mute =cp->un.ord;
   1244 				error = 0;
   1245 			}
   1246 			break;
   1247 
   1248 		case ARIAMIX_LINE_IN_MUTE:
   1249 			if (cp->un.ord == 0 || cp->un.ord == 1) {
   1250 				sc->aria_mix[ARIAMIX_LINE_IN_LVL].mute =
   1251 					cp->un.ord;
   1252 				error = 0;
   1253 			}
   1254 			break;
   1255 
   1256 		case ARIAMIX_CD_MUTE:
   1257 			if (cp->un.ord == 0 || cp->un.ord == 1) {
   1258 				sc->aria_mix[ARIAMIX_CD_LVL].mute = cp->un.ord;
   1259 				error = 0;
   1260 			}
   1261 			break;
   1262 
   1263 		case ARIAMIX_DAC_MUTE:
   1264 			if (cp->un.ord == 0 || cp->un.ord == 1) {
   1265 				sc->aria_mix[ARIAMIX_DAC_LVL].mute =cp->un.ord;
   1266 				error = 0;
   1267 			}
   1268 			break;
   1269 
   1270 		case ARIAMIX_AUX_MUTE:
   1271 			if (cp->un.ord == 0 || cp->un.ord == 1) {
   1272 				sc->aria_mix[ARIAMIX_AUX_LVL].mute =cp->un.ord;
   1273 				error = 0;
   1274 			}
   1275 			break;
   1276 
   1277 		case ARIAMIX_TEL_MUTE:
   1278 			if (cp->un.ord == 0 || cp->un.ord == 1) {
   1279 				sc->aria_mix[ARIAMIX_TEL_LVL].mute =cp->un.ord;
   1280 				error = 0;
   1281 			}
   1282 			break;
   1283 
   1284 		default:
   1285 			/* NOTREACHED */
   1286 			return ENXIO;
   1287 		}
   1288 
   1289 	return error;
   1290 }
   1291 
   1292 int
   1293 aria_mixer_get_port(void *addr, mixer_ctrl_t *cp)
   1294 {
   1295 	struct aria_softc *sc;
   1296 	int error;
   1297 
   1298 	DPRINTF(("aria_mixer_get_port\n"));
   1299 	sc = addr;
   1300 	error = EINVAL;
   1301 
   1302 	/* This could be done better, no mixer still has some controls. */
   1303 	if (!(ARIA_MIXER&sc->sc_hardware))
   1304 		return ENXIO;
   1305 
   1306 	switch (cp->dev) {
   1307 	case ARIAMIX_MIC_LVL:
   1308 		if (cp->type == AUDIO_MIXER_VALUE) {
   1309 			cp->un.value.num_channels =
   1310 				sc->aria_mix[ARIAMIX_MIC_LVL].num_channels;
   1311 			cp->un.value.level[0] =
   1312 				sc->aria_mix[ARIAMIX_MIC_LVL].level[0];
   1313 			cp->un.value.level[1] =
   1314 				sc->aria_mix[ARIAMIX_MIC_LVL].level[1];
   1315 			error = 0;
   1316 		}
   1317 		break;
   1318 
   1319 	case ARIAMIX_LINE_IN_LVL:
   1320 		if (cp->type == AUDIO_MIXER_VALUE) {
   1321 			cp->un.value.num_channels =
   1322 				sc->aria_mix[ARIAMIX_LINE_IN_LVL].num_channels;
   1323 			cp->un.value.level[0] =
   1324 				sc->aria_mix[ARIAMIX_LINE_IN_LVL].level[0];
   1325 			cp->un.value.level[1] =
   1326 				sc->aria_mix[ARIAMIX_LINE_IN_LVL].level[1];
   1327 			error = 0;
   1328 		}
   1329 		break;
   1330 
   1331 	case ARIAMIX_CD_LVL:
   1332 		if (cp->type == AUDIO_MIXER_VALUE) {
   1333 			cp->un.value.num_channels =
   1334 				sc->aria_mix[ARIAMIX_CD_LVL].num_channels;
   1335 			cp->un.value.level[0] =
   1336 				sc->aria_mix[ARIAMIX_CD_LVL].level[0];
   1337 			cp->un.value.level[1] =
   1338 				sc->aria_mix[ARIAMIX_CD_LVL].level[1];
   1339 			error = 0;
   1340 		}
   1341 		break;
   1342 
   1343 	case ARIAMIX_TEL_LVL:
   1344 		if (cp->type == AUDIO_MIXER_VALUE) {
   1345 			cp->un.value.num_channels =
   1346 				sc->aria_mix[ARIAMIX_TEL_LVL].num_channels;
   1347 			cp->un.value.level[0] =
   1348 				sc->aria_mix[ARIAMIX_TEL_LVL].level[0];
   1349 			error = 0;
   1350 		}
   1351 		break;
   1352 	case ARIAMIX_DAC_LVL:
   1353 		if (cp->type == AUDIO_MIXER_VALUE) {
   1354 			cp->un.value.num_channels =
   1355 				sc->aria_mix[ARIAMIX_DAC_LVL].num_channels;
   1356 			cp->un.value.level[0] =
   1357 				sc->aria_mix[ARIAMIX_DAC_LVL].level[0];
   1358 			cp->un.value.level[1] =
   1359 				sc->aria_mix[ARIAMIX_DAC_LVL].level[1];
   1360 			error = 0;
   1361 		}
   1362 		break;
   1363 
   1364 	case ARIAMIX_AUX_LVL:
   1365 		if (cp->type == AUDIO_MIXER_VALUE) {
   1366 			cp->un.value.num_channels =
   1367 				sc->aria_mix[ARIAMIX_AUX_LVL].num_channels;
   1368 			cp->un.value.level[0] =
   1369 				sc->aria_mix[ARIAMIX_AUX_LVL].level[0];
   1370 			cp->un.value.level[1] =
   1371 				sc->aria_mix[ARIAMIX_AUX_LVL].level[1];
   1372 			error = 0;
   1373 		}
   1374 		break;
   1375 
   1376 	case ARIAMIX_MIC_MUTE:
   1377 		if (cp->type == AUDIO_MIXER_ENUM) {
   1378 			cp->un.ord = sc->aria_mix[ARIAMIX_MIC_LVL].mute;
   1379 			error = 0;
   1380 		}
   1381 		break;
   1382 
   1383 	case ARIAMIX_LINE_IN_MUTE:
   1384 		if (cp->type == AUDIO_MIXER_ENUM) {
   1385 			cp->un.ord = sc->aria_mix[ARIAMIX_LINE_IN_LVL].mute;
   1386 			error = 0;
   1387 		}
   1388 		break;
   1389 
   1390 	case ARIAMIX_CD_MUTE:
   1391 		if (cp->type == AUDIO_MIXER_ENUM) {
   1392 			cp->un.ord = sc->aria_mix[ARIAMIX_CD_LVL].mute;
   1393 			error = 0;
   1394 		}
   1395 		break;
   1396 
   1397 	case ARIAMIX_DAC_MUTE:
   1398 		if (cp->type == AUDIO_MIXER_ENUM) {
   1399 			cp->un.ord = sc->aria_mix[ARIAMIX_DAC_LVL].mute;
   1400 			error = 0;
   1401 		}
   1402 		break;
   1403 
   1404 	case ARIAMIX_AUX_MUTE:
   1405 		if (cp->type == AUDIO_MIXER_ENUM) {
   1406 			cp->un.ord = sc->aria_mix[ARIAMIX_AUX_LVL].mute;
   1407 			error = 0;
   1408 		}
   1409 		break;
   1410 
   1411 	case ARIAMIX_TEL_MUTE:
   1412 		if (cp->type == AUDIO_MIXER_ENUM) {
   1413 			cp->un.ord = sc->aria_mix[ARIAMIX_TEL_LVL].mute;
   1414 			error = 0;
   1415 		}
   1416 		break;
   1417 
   1418 	case ARIAMIX_MASTER_LVL:
   1419 		if (cp->type == AUDIO_MIXER_VALUE) {
   1420 			cp->un.value.num_channels =
   1421 				sc->ariamix_master.num_channels;
   1422 			cp->un.value.level[0] = sc->ariamix_master.level[0];
   1423 			cp->un.value.level[1] = sc->ariamix_master.level[1];
   1424 			error = 0;
   1425 		}
   1426 		break;
   1427 
   1428 	case ARIAMIX_MASTER_TREBLE:
   1429 		if (cp->type == AUDIO_MIXER_VALUE) {
   1430 			cp->un.value.num_channels = 2;
   1431 			cp->un.value.level[0] = sc->ariamix_master.treble[0];
   1432 			cp->un.value.level[1] = sc->ariamix_master.treble[1];
   1433 			error = 0;
   1434 		}
   1435 		break;
   1436 
   1437 	case ARIAMIX_MASTER_BASS:
   1438 		if (cp->type == AUDIO_MIXER_VALUE) {
   1439 			cp->un.value.num_channels = 2;
   1440 			cp->un.value.level[0] = sc->ariamix_master.bass[0];
   1441 			cp->un.value.level[1] = sc->ariamix_master.bass[1];
   1442 			error = 0;
   1443 		}
   1444 		break;
   1445 
   1446 	case ARIAMIX_OUT_LVL:
   1447 		if (cp->type == AUDIO_MIXER_VALUE) {
   1448 			cp->un.value.num_channels = sc->sc_chans;
   1449 			cp->un.value.level[0] = sc->sc_gain[0];
   1450 			cp->un.value.level[1] = sc->sc_gain[1];
   1451 			error = 0;
   1452 		}
   1453 		break;
   1454 	case ARIAMIX_RECORD_SOURCE:
   1455 		if (cp->type == AUDIO_MIXER_ENUM) {
   1456 			cp->un.ord = sc->aria_mix_source;
   1457 			error = 0;
   1458 		}
   1459 		break;
   1460 
   1461 	default:
   1462 		return ENXIO;
   1463 		/* NOT REACHED */
   1464 	}
   1465 
   1466 	return error;
   1467 }
   1468 
   1469 int
   1470 aria_mixer_query_devinfo(void *addr, mixer_devinfo_t *dip)
   1471 {
   1472 	struct aria_softc *sc;
   1473 
   1474 	DPRINTF(("aria_mixer_query_devinfo\n"));
   1475 	sc = addr;
   1476 
   1477 	/* This could be done better, no mixer still has some controls. */
   1478 	if (!(ARIA_MIXER & sc->sc_hardware))
   1479 		return ENXIO;
   1480 
   1481 	dip->prev = dip->next = AUDIO_MIXER_LAST;
   1482 
   1483 	switch(dip->index) {
   1484 	case ARIAMIX_MIC_LVL:
   1485 		dip->type = AUDIO_MIXER_VALUE;
   1486 		dip->mixer_class = ARIAMIX_INPUT_CLASS;
   1487 		dip->next = ARIAMIX_MIC_MUTE;
   1488 		strcpy(dip->label.name, AudioNmicrophone);
   1489 		dip->un.v.num_channels = 2;
   1490 		strcpy(dip->un.v.units.name, AudioNvolume);
   1491 		break;
   1492 
   1493 	case ARIAMIX_LINE_IN_LVL:
   1494 		dip->type = AUDIO_MIXER_VALUE;
   1495 		dip->mixer_class = ARIAMIX_INPUT_CLASS;
   1496 		dip->next = ARIAMIX_LINE_IN_MUTE;
   1497 		strcpy(dip->label.name, AudioNline);
   1498 		dip->un.v.num_channels = 2;
   1499 		strcpy(dip->un.v.units.name, AudioNvolume);
   1500 		break;
   1501 
   1502 	case ARIAMIX_CD_LVL:
   1503 		dip->type = AUDIO_MIXER_VALUE;
   1504 		dip->mixer_class = ARIAMIX_INPUT_CLASS;
   1505 		dip->next = ARIAMIX_CD_MUTE;
   1506 		strcpy(dip->label.name, AudioNcd);
   1507 		dip->un.v.num_channels = 2;
   1508 		strcpy(dip->un.v.units.name, AudioNvolume);
   1509 		break;
   1510 
   1511 	case ARIAMIX_TEL_LVL:
   1512 		dip->type = AUDIO_MIXER_VALUE;
   1513 		dip->mixer_class = ARIAMIX_INPUT_CLASS;
   1514 		dip->next = ARIAMIX_TEL_MUTE;
   1515 		strcpy(dip->label.name, "telephone");
   1516 		dip->un.v.num_channels = 1;
   1517 		strcpy(dip->un.v.units.name, AudioNvolume);
   1518 		break;
   1519 
   1520 	case ARIAMIX_DAC_LVL:
   1521 		dip->type = AUDIO_MIXER_VALUE;
   1522 		dip->mixer_class = ARIAMIX_INPUT_CLASS;
   1523 		dip->next = ARIAMIX_DAC_MUTE;
   1524 		strcpy(dip->label.name, AudioNdac);
   1525 		dip->un.v.num_channels = 1;
   1526 		strcpy(dip->un.v.units.name, AudioNvolume);
   1527 		break;
   1528 
   1529 	case ARIAMIX_AUX_LVL:
   1530 		dip->type = AUDIO_MIXER_VALUE;
   1531 		dip->mixer_class = ARIAMIX_INPUT_CLASS;
   1532 		dip->next = ARIAMIX_AUX_MUTE;
   1533 		strcpy(dip->label.name, AudioNoutput);
   1534 		dip->un.v.num_channels = 1;
   1535 		strcpy(dip->un.v.units.name, AudioNvolume);
   1536 		break;
   1537 
   1538 	case ARIAMIX_MIC_MUTE:
   1539 		dip->prev = ARIAMIX_MIC_LVL;
   1540 		goto mute;
   1541 
   1542 	case ARIAMIX_LINE_IN_MUTE:
   1543 		dip->prev = ARIAMIX_LINE_IN_LVL;
   1544 		goto mute;
   1545 
   1546 	case ARIAMIX_CD_MUTE:
   1547 		dip->prev = ARIAMIX_CD_LVL;
   1548 		goto mute;
   1549 
   1550 	case ARIAMIX_DAC_MUTE:
   1551 		dip->prev = ARIAMIX_DAC_LVL;
   1552 		goto mute;
   1553 
   1554 	case ARIAMIX_AUX_MUTE:
   1555 		dip->prev = ARIAMIX_AUX_LVL;
   1556 		goto mute;
   1557 
   1558 	case ARIAMIX_TEL_MUTE:
   1559 		dip->prev = ARIAMIX_TEL_LVL;
   1560 		goto mute;
   1561 
   1562 mute:
   1563 		dip->mixer_class = ARIAMIX_INPUT_CLASS;
   1564 		dip->type = AUDIO_MIXER_ENUM;
   1565 		strcpy(dip->label.name, AudioNmute);
   1566 		dip->un.e.num_mem = 2;
   1567 		strcpy(dip->un.e.member[0].label.name, AudioNoff);
   1568 		dip->un.e.member[0].ord = 0;
   1569 		strcpy(dip->un.e.member[1].label.name, AudioNon);
   1570 		dip->un.e.member[1].ord = 1;
   1571 		break;
   1572 
   1573 	case ARIAMIX_MASTER_LVL:
   1574 		dip->type = AUDIO_MIXER_VALUE;
   1575 		dip->mixer_class = ARIAMIX_OUTPUT_CLASS;
   1576 		dip->next = AUDIO_MIXER_LAST;
   1577 		strcpy(dip->label.name, AudioNvolume);
   1578 		dip->un.v.num_channels = 2;
   1579 		strcpy(dip->un.v.units.name, AudioNvolume);
   1580 		break;
   1581 
   1582 	case ARIAMIX_MASTER_TREBLE:
   1583 		dip->type = AUDIO_MIXER_VALUE;
   1584 		dip->mixer_class = ARIAMIX_EQ_CLASS;
   1585 		strcpy(dip->label.name, AudioNtreble);
   1586 		dip->un.v.num_channels = 2;
   1587 		strcpy(dip->un.v.units.name, AudioNtreble);
   1588 		break;
   1589 
   1590 	case ARIAMIX_MASTER_BASS:
   1591 		dip->type = AUDIO_MIXER_VALUE;
   1592 		dip->mixer_class = ARIAMIX_EQ_CLASS;
   1593 		strcpy(dip->label.name, AudioNbass);
   1594 		dip->un.v.num_channels = 2;
   1595 		strcpy(dip->un.v.units.name, AudioNbass);
   1596 		break;
   1597 
   1598 	case ARIAMIX_OUT_LVL:
   1599 		dip->type = AUDIO_MIXER_VALUE;
   1600 		dip->mixer_class = ARIAMIX_OUTPUT_CLASS;
   1601 		strcpy(dip->label.name, AudioNoutput);
   1602 		dip->un.v.num_channels = 2;
   1603 		strcpy(dip->un.v.units.name, AudioNvolume);
   1604 		break;
   1605 
   1606 	case ARIAMIX_RECORD_SOURCE:
   1607 		dip->mixer_class = ARIAMIX_RECORD_CLASS;
   1608 		dip->type = AUDIO_MIXER_ENUM;
   1609 		strcpy(dip->label.name, AudioNsource);
   1610 		dip->un.e.num_mem = 6;
   1611 		strcpy(dip->un.e.member[0].label.name, AudioNoutput);
   1612 		dip->un.e.member[0].ord = ARIAMIX_AUX_LVL;
   1613 		strcpy(dip->un.e.member[1].label.name, AudioNmicrophone);
   1614 		dip->un.e.member[1].ord = ARIAMIX_MIC_LVL;
   1615 		strcpy(dip->un.e.member[2].label.name, AudioNdac);
   1616 		dip->un.e.member[2].ord = ARIAMIX_DAC_LVL;
   1617 		strcpy(dip->un.e.member[3].label.name, AudioNline);
   1618 		dip->un.e.member[3].ord = ARIAMIX_LINE_IN_LVL;
   1619 		strcpy(dip->un.e.member[4].label.name, AudioNcd);
   1620 		dip->un.e.member[4].ord = ARIAMIX_CD_LVL;
   1621 		strcpy(dip->un.e.member[5].label.name, "telephone");
   1622 		dip->un.e.member[5].ord = ARIAMIX_TEL_LVL;
   1623 		break;
   1624 
   1625 	case ARIAMIX_INPUT_CLASS:
   1626 		dip->type = AUDIO_MIXER_CLASS;
   1627 		dip->mixer_class = ARIAMIX_INPUT_CLASS;
   1628 		strcpy(dip->label.name, AudioCinputs);
   1629 		break;
   1630 
   1631 	case ARIAMIX_OUTPUT_CLASS:
   1632 		dip->type = AUDIO_MIXER_CLASS;
   1633 		dip->mixer_class = ARIAMIX_OUTPUT_CLASS;
   1634 		strcpy(dip->label.name, AudioCoutputs);
   1635 		break;
   1636 
   1637 	case ARIAMIX_RECORD_CLASS:
   1638 		dip->type = AUDIO_MIXER_CLASS;
   1639 		dip->mixer_class = ARIAMIX_RECORD_CLASS;
   1640 		strcpy(dip->label.name, AudioCrecord);
   1641 		break;
   1642 
   1643 	case ARIAMIX_EQ_CLASS:
   1644 		dip->type = AUDIO_MIXER_CLASS;
   1645 		dip->mixer_class = ARIAMIX_EQ_CLASS;
   1646 		strcpy(dip->label.name, AudioCequalization);
   1647 		break;
   1648 
   1649 	default:
   1650 		return ENXIO;
   1651 		/*NOTREACHED*/
   1652 	}
   1653 	return 0;
   1654 }
   1655