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